Where with dynamic parameter (linq)
Yesterday i faced a satiation that i need to make a search in a datatable with dynamic parameter ( i mean parameters that may change - not the type of the parameter –). So i had this table below
ID appID condition
--------- --------- ------------
1 A 25
2 A 35
3 D 35
4 C 25
5 D 45
6 A 15
and i was looking to do this
for example : i would like to know the applications that have conditions 15,25 and 35 so that will be A and C
or 45 , 25 and that will be C and D.
and after some code refactoring i found the best way to code this is as below;
var appIDKeys= new List<Guid>();
listOfConditions.ForEach(a =>
{
var q = (from p in MainTable
where p.ConditionKey== new Guid(a)
select p.AppID).ToList();
if (q.Count > 0)
{
if (appIDKeys.Count == 0)
{
appIDKeys= q;
}
appIDKeys= appIDKeys.Intersect(q).ToList<Guid>();
}
else
{
throw new ApplicationException("No Match");
}
});
Hope This helps