编写谓词以使用实体框架搜索存储库时遇到问题(相当于在 SQL 中使用 Join)

发布于 2024-12-19 03:56:44 字数 1524 浏览 1 评论 0原文

我们使用代码优先 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来提高复杂性,那么它的复杂性就会增加 1 级。我想匹配的名字,这也可能吗?

感谢您的帮助!

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 技术交流群。

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

发布评论

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

评论(2

伤痕我心 2024-12-26 03:56:44

看起来您想要 IEnumerable。 Any

public class ConferenceAttendeeNameSearch : ISpecification<Conference>
{
    public Expression<Func<Conference, bool>> IsSatisfied()
    {
        return a => a.People.Any(p => p.FirstName.Equals("David"));
    }
}

Any 返回一个布尔值,指示序列的任何元素是否与条件匹配。您可以将其行为描述为类似于 sequence.Where(condition).Count() > 0 。

您应该了解的相关方法(尽管它不适用于此处)是 IEnumerable.All

It looks like you want IEnumerable<T>.Any:

public class ConferenceAttendeeNameSearch : ISpecification<Conference>
{
    public Expression<Func<Conference, bool>> IsSatisfied()
    {
        return a => a.People.Any(p => p.FirstName.Equals("David"));
    }
}

Any returns a bool indicating whether any of the sequence's elements matched the condition. You might describe its behavior as similar to sequence.Where(condition).Count() > 0.

A related method you should know about (though it doesn't apply here) is IEnumerable<T>.All.

狼性发作 2024-12-26 03:56:44

试试这个:

public class ConferenceAttendeeNameSearch : ISpecification<Conference>
{
    public Expression<Func<Conference, bool>> IsSatisfied()
    {
        return a => a.People.Where(p => p.FirstName.Equals("David"));
    }
}

Try this:

public class ConferenceAttendeeNameSearch : ISpecification<Conference>
{
    public Expression<Func<Conference, bool>> IsSatisfied()
    {
        return a => a.People.Where(p => p.FirstName.Equals("David"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文