开发者

Combine delegates with an OR operator

开发者 https://www.devze.com 2023-04-08 02:18 出处:网络
Looking up values in two different tables with LinqToSQL, and combining the results based on what is found in what resultset.

Looking up values in two different tables with LinqToSQL, and combining the results based on what is found in what resultset.

How can I combine two Funcs based on given conditions:

 // a filter for a LinqToSQL query:
 Func<ComputerDetail, bool> filter;

 // declare the default filter
 filter = x => x.ComputerName.Contains(searchTerm);

then if a condition is met it would like to add the below lambda to the above one:

 x => computernames.Contains(x.ComputerName)

so it both would result in:

 x => computernames.Contains(x.Com开发者_如何转开发puterName)) ||
      x.ComputerName.Contains(searchTerm);

Creating a multicast delegate with a plus operator almost seems to do it, however, I am not sure how to apply the OR (||) in this case:

 filter += computernames.Contains(x.ComputerName) 

note: computernames is a List<String> , ComputerName is a property of ComputerDetail which is a Class representing a View in LinqToSQL


If it's LINQ to SQL, you don't want to combine two delegates - you want to combine two expression trees. Otherwise all of your filtering will be done on the client side.

If that's the case, I believe you basically just need PredicateBuilder - use predicate1.Or(predicate2) to combine two predicates.

If you really want the delegate form, you can take the same code and just change all the signatures to use delegates instead of expression trees.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号