具有两个表的谓词构建器
一方可以有一个或多个联系人对象。
我想选择街道名称包含特定关键字的所有当事人。
如果我只想在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();
}
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关键是您对一个派对有多个联系人,因此您应该决定是否要在“任何联系人与街道名称匹配”或“所有联系人与街道名称匹配”的情况下拥有一个派对。
对于第一种情况,它将是:
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:
如果您的模型将联系人作为当事人对象上的关联对象(并且没有一对多关系),则此方法将起作用
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)