且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何重构多个类似的LINQ查询?

更新时间:2023-11-14 08:58:40

病程中出现。在其中,参数类型的只是一个简单的封闭 Func键< T,布尔> (其中T是你的数据库的项目类型 - 我不知道出来的$的C $ C),你可以把它包装成一个(匿名)函数。

Of couse there is. The where parameter is just a simple closure of type Func<T, bool> (where T is the type of your DB items - I don't know them out of your code) and you can wrap it into a (anonymous) function.

Func<Func<T, bool>, IEnumerable<T>> MakeQuery = (Func<T, bool> whereParam) => db.TableAs.Where(whereParam).Take(10);

使用像这样

var query1 = MakeQuery(a => a.TableBS.Count() > someValue1);