编写谓词以使用实体框架搜索存储库时遇到问题(相当于在 SQL 中使用 Join)
我们使用代码优先 Entity Framework 4.1 来保存数据。我们的一些实体具有这样的关系:
[Serializable]
public class Conference
{
private IList<Attendee> people;
private string name;
public IList<Attendee> People
{
get { return this.team; }
private set { this.team = value; }
}
public string Name
{
get { return this.name; }
private set { this.name = value; }
}
}
[Serializable]
public class Attendee
{
private string firstname;
private string surname;
public string Firstname
{
get { return this.firstname; }
private set { this.firstname = value; }
}
public string Surname
{
get { return this.surname; }
private set { this.surname = value; }
}
}
我想使用 LINQ 查询我们的会议存储库。我可以使用这样的搜索规范通过名称轻松找到会议:
public class ConferenceNameSearch : ISpecification<Conference>
{
public Expression<Func<Conference, bool>> IsSatisfied()
{
return a => a.Name == "Christmas Party";
}
}
但是我在编写搜索规范时遇到了真正的麻烦,该搜索规范将返回至少有 1 个名字为“David”的人参加的所有会议:
public class ConferenceAttendeeNameSearch : ISpecification<Conference>
{
public Expression<Func<Conference, bool>> IsSatisfied()
{
return a => a.People.Contains( ???? err "David";
}
}
有没有办法做这个吗?另外,如果我不只是搜索与会者名为“David”的会议,而是使用 List
感谢您的帮助!
We are using code first Entity Framework 4.1 to persist data. Some of our entities have a relationship like this:
[Serializable]
public class Conference
{
private IList<Attendee> people;
private string name;
public IList<Attendee> People
{
get { return this.team; }
private set { this.team = value; }
}
public string Name
{
get { return this.name; }
private set { this.name = value; }
}
}
[Serializable]
public class Attendee
{
private string firstname;
private string surname;
public string Firstname
{
get { return this.firstname; }
private set { this.firstname = value; }
}
public string Surname
{
get { return this.surname; }
private set { this.surname = value; }
}
}
I would like to query our repository of conferences using LINQ. I can find a conference easily by name using a search specification like this:
public class ConferenceNameSearch : ISpecification<Conference>
{
public Expression<Func<Conference, bool>> IsSatisfied()
{
return a => a.Name == "Christmas Party";
}
}
But I am having real trouble writing a search specification that would return all conferences attended by at least 1 person who has the first name "David":
public class ConferenceAttendeeNameSearch : ISpecification<Conference>
{
public Expression<Func<Conference, bool>> IsSatisfied()
{
return a => a.People.Contains( ???? err "David";
}
}
Is there a way to do this? Also, taking it up 1 more level of complexity, if instead of just searching for conferences with an attendee called "David" I had a List<string> names that I wanted to match, would this be possible too?
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来您想要
IEnumerable。 Any
:Any
返回一个布尔值,指示序列的任何元素是否与条件匹配。您可以将其行为描述为类似于sequence.Where(condition).Count() > 0 。
您应该了解的相关方法(尽管它不适用于此处)是
IEnumerable.All
。It looks like you want
IEnumerable<T>.Any
:Any
returns a bool indicating whether any of the sequence's elements matched the condition. You might describe its behavior as similar tosequence.Where(condition).Count() > 0
.A related method you should know about (though it doesn't apply here) is
IEnumerable<T>.All
.试试这个:
Try this: