导航属性上的动态 Linq 搜索表达式
我们正在使用 Dynamic Linq 库构建动态搜索表达式。我们遇到了一个问题,如何使用动态 linq 库构建具有一对多关系的导航属性的lambda表达式。
我们将以下内容与 contains 语句一起使用 -
Person.Names.Select(FamilyName).FirstOrDefault()
它可以工作,但有两个问题。
它当然只选择 FirstOrDefault() 名称。我们希望它使用每个人的所有名称。
如果没有人员姓名,则 Select 会引发异常。
对于常规查询来说并不困难,因为我们可以执行两个 from 语句,但 lambda 表达式更具挑战性。
任何建议将不胜感激。
编辑- 附加代码信息...非动态 linq 表达式看起来像这样。
var results = persons.Where(p => p.Names.Select(n => n.FamilyName).FirstOrDefault().Contains("Smith")).ToList();
该类如下所示-
public class Person
{
public bool IsActive { get; set;}
public virtual ICollection<Name> Names {get; set;}
}
public class Name
{
public string GivenName { get; set; }
public string FamilyName { get; set; }
public virtual Person Person { get; set;}
}
We are building dynamic search expressions using the Dynamic Linq library. We have run into an issue with how to construct a lamba expression using the dynamic linq library for navigation properties that have a one to many relationship.
We have the following that we are using with a contains statement-
Person.Names.Select(FamilyName).FirstOrDefault()
It works but there are two problems.
It of course only selects the FirstOrDefault() name. We want it to use all the names for each person.
If there are no names for a person the Select throws an exception.
It is not that difficult with a regular query because we can do two from statements, but the lambda expression is more challenging.
Any recommendations would be appreciated.
EDIT-
Additional code information...a non dynamic linq expression would look something like this.
var results = persons.Where(p => p.Names.Select(n => n.FamilyName).FirstOrDefault().Contains("Smith")).ToList();
and the class looks like the following-
public class Person
{
public bool IsActive { get; set;}
public virtual ICollection<Name> Names {get; set;}
}
public class Name
{
public string GivenName { get; set; }
public string FamilyName { get; set; }
public virtual Person Person { get; set;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们经过深思熟虑并做出了它,但这非常具有挑战性。以下是我们如何取得最终结果的各种方法。现在我们只需要重新思考我们的 SearchExpression 类是如何构建的......但那是另一个故事了。
1.等效查询语法
2.等效的 Lambda 语法
3.与 Dynamic Linq 等效的 Lambda 语法
注释 - 您必须向 Dynamic Linq 库添加 Contains 方法。
编辑-或者仅使用选择...更简单...但它需要添加 Contains 方法,如上所述。
我们最初尝试过此操作,但遇到了可怕的“不存在适用的聚合方法 Contains”。错误。当我们试图让 SelectMany 工作时,我们用一种迂回的方式解决了这个问题......因此只是回到了 Select 方法。
We hashed it out and made it, but it was quite challenging. Below are the various methods on how we progressed to the final result. Now we just have to rethink how our SearchExpression class is built...but that is another story.
1. Equivalent Query Syntax
2. Equivalent Lambda Syntax
3. Equivalent Lambda Syntax with Dynamic Linq
Notes - You will have to add a Contains method to the Dynamic Linq library.
EDIT - Alternatively use just a select...much more simple...but it require the Contains method addition as noted above.
We originally tried this, but ran into the dreaded 'No applicable aggregate method Contains exists.' error. I a round about way we resolved the problem when trying to get the SelectMany working...therefore just went back to the Select method.