导航属性上的动态Linq搜索表达式 [英] Dynamic Linq Search Expression on Navigation Properties

查看:65
本文介绍了导航属性上的动态Linq搜索表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Dynamic Linq库构建动态搜索表达式.关于如何使用动态linq库构造具有一对多关系的导航属性的lamba表达式,我们遇到了一个问题.

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.

  1. 它当然仅选择FirstOrDefault()名称.我们希望它使用每个人的所有姓名.

  1. It of course only selects the FirstOrDefault() name. We want it to use all the names for each person.

如果没有人的名字,则Select会引发异常.

If there are no names for a person the Select throws an exception.

对于常规查询而言,这并不困难,因为我们可以从from语句中进行两个操作,但是lambda表达式更具挑战性.

It is not that difficult with a regular query because we can do two from statements, but the lambda expression is more challenging.

任何建议将不胜感激.

编辑- 其他代码信息...一个非动态的linq表达式看起来像这样.

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();

该类如下所示-

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;}
}

推荐答案

我们对它进行了哈希处理并使其成为现实,但这颇具挑战性.以下是关于如何逐步达到最终结果的各种方法.现在我们只需要重新考虑我们的SearchExpression类是如何构建的……但这是另一回事了.

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.等效查询语法

var results = from person in persons
from name in person.names
where name.FamilyName.Contains("Smith")
select person;

2.等效Lambda语法

var results = persons.SelectMany(person => person.Names)
                     .Where(name => name.FamilyName.Contains("Smith"))
                     .Select(personName => personName.Person);

3.具有动态Linq的等效Lambda语法

var results = persons.AsQueryable().SelectMany("Names")
                     .Where("FamilyName.Contains(@0)", "Smith")
                     .Select("Person");

注释-您将必须向动态Linq库中添加一个Contains方法.

Notes - You will have to add a Contains method to the Dynamic Linq library.

编辑-另外,也可以仅使用选择...更简单...但是它需要如上所述的Contains方法添加.

EDIT - Alternatively use just a select...much more simple...but it require the Contains method addition as noted above.

var results = persons.AsQueryable().Where("Names.Select(FamilyName)
                                   .Contains(@0", "Smith)

我们最初尝试过此方法,但遇到了可怕的不存在任何适用的聚合方法包含".错误.在尝试使SelectMany正常工作时,我解决了问题的方法,因此仅返回到Select方法.

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.

这篇关于导航属性上的动态Linq搜索表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆