Muhanad YOUNIS

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

Posted: Oct 20 2009, 11:03 AM by mohi88 | with 2 comment(s) |
Filed under: ,

Comments

Samer Chidiac said:

Good Stuff!!

Keep up the Good Work Muhanad!

SC

# October 20, 2009 7:09 AM

Bander Al Fuqaha' said:

Good stuff amigo

# October 21, 2009 9:51 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)