使用动态搜索防爆pressions的EF问题动态、问题、pressions、EF

2023-09-05 23:33:39 作者:唯有烟对我不离不弃

我目前正在使用的数据结构类似以下内容:

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库。如何做到这一点可以在这里找到。 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