实体框架多对多与连接表

时间:2022-10-05 12:02:37

I have two tables, in a many-to-many relationship, with a junction table, as follows:

我有两个表,在多对多关系中,有一个连接表,如下所示:

Member       MemberGroup        Group
=========    ============       =======
PK | ID      PK | ID            PK | ID
   | Name       | Member           | Name
                | Group
                | MemberSince

I need to add all the members of a specific group to a list box. The group is selected from a data bound combo box. I was looking to do something like this:

我需要将特定组的所有成员添加到列表框中。从数据绑定组合框中选择组。我想做这样的事情:

listbox1.ItemsSource = DataModel.Members.Where(u=>u.Group == mygroup);

However, the Member entity only contains the MemberGroup entries.... not the actual groups.

然而,会员实体只包含MemberGroup条目....不实际的组。

What is the best way to do this?

最好的方法是什么?

By the way, .NET Framework 3.5, WPF, Entity Framework, C#, SQL Server Compact Edition (2008)

顺便说一下。net Framework 3.5, WPF,实体框架,c#, SQL Server Compact Edition (2008)

1 个解决方案

#1


1  

Found the solution.

找到了解决方案。

public partial class Group
{
    public ObjectQuery<Member> Members
    {
        get
        {
            return (from j in DataModel.MemberGroup
                    where j.Group.ID == this.ID
                    select j.Member) as ObjectQuery<Member>;
        }
    }
}

#1


1  

Found the solution.

找到了解决方案。

public partial class Group
{
    public ObjectQuery<Member> Members
    {
        get
        {
            return (from j in DataModel.MemberGroup
                    where j.Group.ID == this.ID
                    select j.Member) as ObjectQuery<Member>;
        }
    }
}