如何使用表达式将 classProperty 传递给 LINQ
我有一个 LINQ 查询,我想将 Person 参数传递给它。大概应该是这样的。
Expression<Func<Person, long>> exp1 = person.CarId;
Expression<Func<Person, long>> exp2 = person.PetId;
var result = db.people.Select(x => new {PersonName = x.Name, EntityId = exp1}).ToList()
我该怎么做呢?
I have a LINQ query, and I want to pass Person parameters to it. Probably it should be something like this.
Expression<Func<Person, long>> exp1 = person.CarId;
Expression<Func<Person, long>> exp2 = person.PetId;
var result = db.people.Select(x => new {PersonName = x.Name, EntityId = exp1}).ToList()
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用 IQueryble 来重用部分查询。它类似于以下内容:
I've used IQueryble to reuse parts of queries. It was something like the following:
Vanilla EF 不允许此类查询。我建议使用 LINQKit。只需要配置
DbContextOptions
:然后您可以通过以下方式使用您的表达式:
Vanilla EF do not allow such queries. I would suggest to use LINQKit. It needs just configuring
DbContextOptions
:Then you can use your expressions in the following way: