LINQ dynamic query

Posted by ile on Stack Overflow See other posts from Stack Overflow or by ile
Published on 2010-05-13T14:11:19Z Indexed on 2010/05/13 16:24 UTC
Read the original article Hit count: 362

Filed under:
|
|

How to dynamically generate LINQ query:

int[] IDArray = {55, 36};

public IQueryable<ContactListView> FindAllContacts(int loggedUserID, int[] IDArray)
{
    var result = (
                 from contact in db.Contacts

                 //Start of dynamic part...
                 where 
                 contact.UserID == loggedUserID
                 foreach (var id in IDArray)
                 {
                     where contact.UserID == id 
                 }
                 // End of dynamic part

                 orderby contact.ContactID descending                         
                 select new ContactListView
                 {
                     ContactID = contact.ContactID,
                     FirstName = contact.FirstName,
                     LastName = contact.LastName                            
                 });

    return result;
}

Thanks,
Ile

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about dynamic