How
can i achieve 'IN' operator of sql like functionality in Linq. there is
a contains function in linq but it is not working with the column of Integer type that i have in the database.
please help
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Priyanka22 reputationPosted Mar 17, 2010, 4:36 AM EDT
You can try like:
1.)create a list of all the elemnts to check within IN Operator
2.)Check for each element in linq with CONTAINS.
for eg.
Dictionary<string, string> MyList;
MyList = new Dictionary<string, string>();
MyList.Add("1", "One");
MyList.Add("2", "Two");
MyList.Add("3", "Three");
MyList.Add("4", "Four");
MyList.Add("5", "Five");
MyList.Add("6", "six");
MyList.Add("7", "Seven");
MyList.Add("8", "Eight");
MyList.Add("9", "Nine");
MyList.Add("10", "Ten");
List<string> checkList = new List<string>();
checkList.Add("Two");
checkList.Add("Five");
List<string> selectedValues = (from element in MyList.Values
where n.Contains(element)
select element).ToList();
Then selectedValues List will contain the desired values.!!!
Hope this helps.!!! :)
Amit ChoudharyRank #145 · 15.8K reputationPosted Mar 17, 2010, 5:01 AM EDT
See below code:
var Booths = new[] { new { BoothName = "Booth1", PinCode = 411011 }, new { BoothName = "Booth2", PinCode = 411021}, new { BoothName = "Booth3", PinCode = 411029 }, new { BoothName = "Booth4", PinCode = 411044 }, new { BoothName = "Booth5", PinCode = 411056 }, new { BoothName = "Booth6", PinCode = 411023 }, new { BoothName = "Booth7", PinCode = 411024 }var whereAmI = from booth in Booths join pins in pinCodes on booth.PinCode equals pins select booth;Please mark as answer if it helps.
naveen gupta12 reputationPosted Mar 17, 2010, 5:37 AM EDT
var query = (from user in vsDataContext.user_registrationDetails
join cty in vsDataContext.Countries on user.countryid equals cty.CountryId
join state in vsDataContext.States on user.stateId equals state.StateId
join rgn in vsDataContext.Religions on user.religionId equals rgn.religionId
join mothertungue in vsDataContext.LanguageMotherTongues on user.motherTongueId equals mothertungue.languageMotherTongueId
join educ in vsDataContext.EducationDetails on user.educationId equals educ.educationId
join marriage in vsDataContext.MarriageStatus on user.marriageStatusId equals marriage.marriageStatusId
where user.gender == char.Parse(gender) &&
user.userAge >= int.Parse(Wucage1.minAge) &&
user.userAge <= int.Parse(Wucage1.maxAge) &&
rgn.religionId == (religion == 1 ? rgn.religionId : religion) &&
user.countryid.ToString().Contains(location)
select new
{
user.username,
user.userId,
user.firstName,
user.userAge,
rgn.religionName,
cty.CountryName,
state.StateName,
mothertungue.languageMotherTongueName,
user.aboutUser,
user.userImage
});
In the where cluase there is column countryid which is of integer type in database and we provide it the value from the location which contain multiple records seprated by comma operator
naveen gupta12 reputationPosted Mar 18, 2010, 4:56 AM EDT