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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
naveen guptaPosted Mar 18, 2010, 8:56 AM
naveen guptaPosted Mar 17, 2010, 9:37 AM
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
Amit ChoudharyPosted Mar 17, 2010, 9:01 AM
See below code:
var pinCodes = new[] { 411021, 411029, 411044 };
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 }
Please mark as answer if it helps.
PriyankaPosted Mar 17, 2010, 8:36 AM
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.!!! :)