EF 中动态搜索表达式的问题

发布于 2024-10-19 01:08:20 字数 1024 浏览 1 评论 0原文

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

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

我正在尝试使用一些动态搜索表达式从我的表示层传递到存储库级别(以实际应用搜索)。

但是,由于个人可以有 1-M 个个人名称,我遇到了一些问题,并且我尝试使用 LINQ 来获取个人的所有个人名称,以便可以查询它们。

例如 - 这就是表达式当前的样子:

searchExpressions.Add(new SearchExpression("Individual
                                           .IndividualNames
                                           .Select(GivenName)
                                           .FirstOrDefault()"
             , ComparisonOperator.Contains, "Test");

当前仅确定第一个 individualName 实例中的 GiveName 是否包含“Test”。上面的内容按其应有的方式工作 - 然而,我对如何确定任何 individualNames 是否包含该字符串感到有点困惑。

任何帮助将不胜感激 - 因为我已经尝试了几件事但没有任何运气。

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

and

public class IndividualName
{
    public string FamilyName {get; set;}
    public string GivenName {get; set;}
    public string MiddleName {get; set;}
}

I am attempting to use some Dynamic search expressions to pass from my presentation layer to repository level (to actually apply the search).

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.

For example's sake - this is what the expression currently looks like:

searchExpressions.Add(new SearchExpression("Individual
                                           .IndividualNames
                                           .Select(GivenName)
                                           .FirstOrDefault()"
             , ComparisonOperator.Contains, "Test");

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.

Any help would be appreciated - as I have tried several things without any luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2024-10-26 01:08:20

我想您会寻找...。

searchExpressions.Add(new SearchExpression("Individual
                                            .IndividualNames
                                            .Select(GivenName)",
                      ComparisonOperator.Contains, "Test");

您还需要将 Contains Aggregate Method 添加到 Dynamic Linq 库。如何执行此操作可以在此处找到。
http: //blog.walteralmeida.com/2010/05/advanced-linq-dynamic-linq-library-add-support-for-contains-extension-.html

I think you would be looking for....

searchExpressions.Add(new SearchExpression("Individual
                                            .IndividualNames
                                            .Select(GivenName)",
                      ComparisonOperator.Contains, "Test");

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

孤独难免 2024-10-26 01:08:20

我不确定这是否适用于您的情况,但也许这个 lambda ?

Individual.IndividualNames.Where(x => x.GivenName == "Test")

im not sure this is aplicable in your case, but maybe this lambda?

Individual.IndividualNames.Where(x => x.GivenName == "Test")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文