具有多对多 firstordefault 的 mvc 索引

mvc index with many to many firstordefault

我遇到了一个问题,我觉得我忽略了什么。

我正在尝试从 asp.net mvc 索引视图的一对多关系中获得一对一结果。

我有三个类

public class Contact
{
    public int ContactID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Relation> Relations { get; set; }
}

public class Relation
{
    public int RelationID { get; set; }
    public string Position { get; set; }
    public int CompanyID { get; set; }
    public int? ContactID { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
}

public class Company
{
    public int CompanyID { get; set; }
    public string CompanyName { get; set; }
    public virtual ICollection<Relation> Relations { get; set; }
}

结果应包括以下内容

public class ContactViewModel
{
    public int ContactID { get; set; }
    public string Name { get; set; }
    public string Position { get; set; }
    public string CompanyName { get; set; }
}

我已尝试使用 LINQ 和 Lambda 表达式将数据导入视图模型,例如

        var contacts = from contact in db.Contacts
                   .Include(relation => relation.Relation
                        .Where(s => s.ContactID == contact.ContactID)
                        .OrderByDescending(d => d.StartDate)
                        .FirstOrDefault())
                   select new ContactViewModel
                    {
                        ContactID = contact.ContactID,
                        Name = contact.Name,
                        Position = relation.Position,
                        Company = relation.Company.CompanyName
                    };

但我没有让它工作...

视图应显示 asp.net mvc 索引视图

Name  Position   Company
Hans  Boss       Sausage House
Tom   Manager    Burger Building

查询关系没问题,得到相关的Contacts和Companies,但不是每个Contact都有职位,有的不止一个。所以我想包括所有联系人和所有当前关系。

我知道更改关系或更改视图的外观会更容易。

但也许有人可以提供帮助。

谢谢!

更新

感谢您的快速回复,您的评论绝对正确。但我的问题不是查询的编译。这个查询无论如何都行不通。我不知道如何获得一个充满所需结果的模型,以及是否可以这样做。我可能不得不采取不同的方法,我已经尝试了一些。也许有人可以帮助进行具有所需结果的查询。

为新方法编辑了我以前的代码:

var contacts = from relation in db.Relations
    join contact in db.Contacts on relation.ContactID equals contact.ContactID
    join company in db.Companies on relation.CompanyID equals company.CompanyID
    orderby relation.StartDate descending
    select new ContactViewModel
    {
        ContactID = contact.ContactID,
        Name = contact.Name,
        Position = relation.Position,
        Company = company.CompanyName
    };

虽然我不确定我是否理解了定义 public virtual ICollection<Relation> Relation { get; set; } 正确。

有关详细说明,请参阅 MSDN 文档:

画了一张小图,我想我也要把它留在这里以作澄清:

如果最终结果是投影,则不能使用 Include,并且不能过滤 Included 个表达式。

但是,如果您使用 let 关键字,这很容易:

var contacts = from contact in db.Contacts
               let relation = contact.Relation
                              .OrderByDescending(d => d.StartDate)
                              .FirstOrDefault()
               select new ContactViewModel
               {
                   ContactID = contact.ContactID,
                   Name = contact.Name,
                   Position = relation.Position,
                   Company = relation.Company.CompanyName
               };

顺便说一句,帮自己一个忙,使用复数名词来收集 属性 个名字。