Select record from database with Join and where conditions using Linq query in ASP.Net

Last Reply one year ago By dharmendr

Posted one year ago

userlist1 showing all user values, but userlist2 not showing value.

Can i apply condition where direct to userlist1 how?

string dob2 = dobyear2 + "-" + month + "-" + 1;
DateTime dob22 = Convert.ToDateTime(dob2);
List<User_Detail> userlist = new List<User_Detail>();
// userlist = db.User_Detail.Where(s => s.Gender == 2 && s.DOB <= dob21 && s.DOB >= dob22).ToList();
var userlist1 = (from u in db.User_Detail

                join st in db.State_Name on u.State equals st.State_Id

                join h in db.Height_Table on u.Height equals h.Height_Id

                join e in db.Education_Qualification on u.Education_Qualification equals e.Education_Id

                join leng in db.Mother_Tongue on u.Mother_Tongue equals leng.Language_id

                join rlgn in db.Religions on u.Religion equals rlgn.Religion_Id

                join cast in db.Castes on u.Caste equals cast.Caste_Id
                join incm in db.AnnuaIncome_Table on u.Annual_Income equals incm.Income_Id

                select new users_details
                    First_Name = u.First_Name,
                    Last_Name = u.Last_Name,
                    Annual_IncomeName = incm.Annual_Income,
                    Caste1 = cast.Caste1,
                    Religion_Name = rlgn.Religion_Name,
                    Language = leng.Language,
                    EducationName = e.Education,
                    StateName = st.State

var userlist2 = userlist1.Where(s => s.Gender == 2 && s.DOB <= dob21 && s.DOB >= dob22).ToList();              



Posted one year ago


You need to add where condition before selecting the columns.

Refer below link for adding where condition to multiple join query using link.

Linq query to join multiple tables from database in ASP.Net