Using one LINQ statement with different parameters

Posted by Brettski on Stack Overflow See other posts from Stack Overflow or by Brettski
Published on 2010-03-13T19:44:15Z Indexed on 2010/03/13 19:45 UTC
Read the original article Hit count: 289

Filed under:

I have a pretty complex linq statement I need to access for different methods. Each of these methods may need to see the resulting data with different parameters. For one method it may be a project code, for another it may be language. The statement is pretty much the same it's just the where part which changes.

I have not been able to figure out how to use different where statements without duplicating the entire linq statement, and that just isn't dry enough for me.

For example (greatly simplified):

var r = from c in  customer
        where c.name == "some name"

// or it may be 

var r = from c in customer
        where c.customerId == 8

Is there a way to have both of these in the same statement so I can use one or the other based on what I am doing? I tried an if statement to use one of the where statements or the other, and that didn't go over very well.

© Stack Overflow or respective owner

Related posts about linq-to-sql