使用动态搜索防爆pressions的EF问题 [英] Issues with Dynamic Search Expressions in EF
问题描述
我目前正在使用的数据结构类似以下内容:
I currently am using a data structures similar to the following:
public class Individual
{
//Other properties omitted for brevity sake
public List<IndividualName> IndividualNames {get; set;}
}
和
public class IndividualName
{
public string FamilyName {get; set;}
public string GivenName {get; set;}
public string MiddleName {get; set;}
}
我试图用一些动态搜索EX pressions从我的presentation层传递到存储库级别(实际应用搜索)。
I am attempting to use some Dynamic search expressions to pass from my presentation layer to repository level (to actually apply the search).
不过,我遇到了一些问题,因为事实上,一个人可以有1-M个人姓名,我试图使用LINQ抓住所有的个人IndividualNames这样他们就可以进行查询。
However, I have run into some issues due to the fact that an Individual can have 1-M Individual Names, and I am trying to use LINQ to grab all of an Individual's IndividualNames so they can be queried.
例如的缘故 - 这是前pression目前是这样的:
For example's sake - this is what the expression currently looks like:
searchExpressions.Add(new SearchExpression("Individual
.IndividualNames
.Select(GivenName)
.FirstOrDefault()"
, ComparisonOperator.Contains, "Test");
这将目前只确定GivenName上的第一IndividualName实例包含测试。以上作品,因为它应该 - 但我有点卡住了我会怎样能够确定条款是否有任何IndividualNames包含字符串
This will currently only determine if the GivenName in the first IndividualName instance Contains "Test". The above works as it should - however I am a bit stuck in terms of how I would be able to determine if Any of the IndividualNames contained the string.
任何帮助将是AP preciated - 因为我已经试过几件事情没有任何的运气
Any help would be appreciated - as I have tried several things without any luck.
推荐答案
我想你要寻找......
I think you would be looking for....
searchExpressions.Add(new SearchExpression("Individual
.IndividualNames
.Select(GivenName)",
ComparisonOperator.Contains, "Test");
您还需要添加一个包含聚合方法,以动态的LINQ库。如何做到这一点可以在这里找到。 <一href="http://blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html" rel="nofollow">http://blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html
You will also need to add a Contains Aggregate Method to the Dynamic Linq library. How to do this can be found here. http://blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html
这篇关于使用动态搜索防爆pressions的EF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!