具有两个表的谓词构建器

发布于 2024-11-15 06:58:47 字数 1706 浏览 4 评论 0原文

在此处输入图像描述

一方可以有一个或多个联系人对象。

我想选择街道名称包含特定关键字的所有当事人
如果我只想在Party中搜索,我可以使用下面的代码。但如何将其扩展为在联系人中进行搜索?

public IQueryable<Party> SearchParties(List<string> keywords)
    {
        var predicate = PredicateBuilder.False<Party>();

        foreach (string word in keywords)
        {
            var keyword = word;
            predicate = predicate.Or(p => p.surname.Contains(keyword));
            predicate = predicate.Or(p => p.lastname.Contains(keyword));
            predicate = predicate.Or(p => p.number.Contains(keyword));
        }
        return db.Parties.Where(predicate);
    }  

还有什么您需要知道的吗?

编辑
我想我可以创建另一个谓词,然后再加入它们。类似于:

var predicate2 = PredicateBuilder.False<Contact>();  

...在 foreach 中:

predicate2 = predicate2.Or(p => p.streetname.Contains(keyword));  

但是在返回之前我将如何连接 predicate 和 predicate2 ?

编辑2
或者,在执行谓词构建器之前加入派对联系人

编辑3
以下是生成的类的部分内容:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Contact")]
public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged
{
    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _id;  
    // ...more properties  
    private EntityRef<Party> _Party;

    public Contact()
    {
    this._Party = default(EntityRef<Party>);
    OnCreated();
    }
}

enter image description here

A Party can have one or more Contact objects.

I want to select all Parties who's streetname contains a specific keyword.
If I just want to search in Party I can use the code below. But how do I extend it to also search in Contact?

public IQueryable<Party> SearchParties(List<string> keywords)
    {
        var predicate = PredicateBuilder.False<Party>();

        foreach (string word in keywords)
        {
            var keyword = word;
            predicate = predicate.Or(p => p.surname.Contains(keyword));
            predicate = predicate.Or(p => p.lastname.Contains(keyword));
            predicate = predicate.Or(p => p.number.Contains(keyword));
        }
        return db.Parties.Where(predicate);
    }  

Is there anything else you need to know?

EDIT
I guess I could create another predicate and then join them afterwards. Something like:

var predicate2 = PredicateBuilder.False<Contact>();  

...and in the foreach:

predicate2 = predicate2.Or(p => p.streetname.Contains(keyword));  

But how would I join predicate and predicate2 before returning?

EDIT2
Or, join the Party and Contact before doing the predicate Builder?

EDIT3
Here are parts of the generated classes:

[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Contact")]
public partial class Contact : INotifyPropertyChanging, INotifyPropertyChanged
{
    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _id;  
    // ...more properties  
    private EntityRef<Party> _Party;

    public Contact()
    {
    this._Party = default(EntityRef<Party>);
    OnCreated();
    }
}

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

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

发布评论

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

评论(2

好听的两个字的网名 2024-11-22 06:58:47

关键是您对一个派对有多个联系人,因此您应该决定是否要在“任何联系人与街道名称匹配”或“所有联系人与街道名称匹配”的情况下拥有一个派对。

对于第一种情况,它将是:

predicate = predicate.Or(p => p.Contacts.Any(c => c.streetname.Contains(keyword))));

The point is that you have multiple Contacts to a single Party, hence you should decide if you want to have a Party if "any of the Contacts match the street name" or "all of the Contacts match the street name".

For the first case it would be:

predicate = predicate.Or(p => p.Contacts.Any(c => c.streetname.Contains(keyword))));
囚我心虐我身 2024-11-22 06:58:47

如果您的模型将联系人作为当事人对象上的关联对象(并且没有一对多关系),则此方法将起作用

predicate = predicate.Or(p => p.Contact.streetname.Contains(keyword));

This will work if your model has Contact as an associated object on the party object (and if it doesn't have a one-to-many relation)

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