C# 视图MVC EF6中的重复行
我的视图中有重复的行。我想要一个显示用户所有数据(电子邮件、电话、地址)的视图。当我点击一个有两个不同地址、电话号码和电子邮件的用户时,我的列表中有8个东西,所以我的viewmodel有8个对象 下面是我如何查询数据库的C# 视图MVC EF6中的重复行,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我的视图中有重复的行。我想要一个显示用户所有数据(电子邮件、电话、地址)的视图。当我点击一个有两个不同地址、电话号码和电子邮件的用户时,我的列表中有8个东西,所以我的viewmodel有8个对象 下面是我如何查询数据库的 public ActionResult View_All(int? id) { var cape = from c in db.Constituent join ca in db.Consti
public ActionResult View_All(int? id)
{
var cape = from c in db.Constituent
join ca in db.ConstituentAddress on c.ConstituentId equals ca.ConstituentId
join cp in db.ConstituentPhone on c.ConstituentId equals cp.ConstituentId
join ce in db.ConstituentEmail on c.ConstituentId equals ce.ConstituentId
where ca.ConstituentId == id
&& c.ConstituentId == id
&& cp.ConstituentId == id
&& ce.ConstituentId == id
select new ConstituentDetailsView
{
zConstituent = c,
zConstituentAddress = ca,
zAddress = ca.Address,
zConstituentEmail = ce,
zEmail = ce.Email,
zConstituentPhone = cp,
zPhone = cp.Phone
};
return View("ConstituentDetails/View_All", cape);
}
视图:
我想我需要一个ViewModel,更符合以下内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CRM.DomainClasses;
namespace CodeFirstShenanigansWeb.Models
{
public class ConstituentListDetailsView
{
public Constituent zConstituent { get; set; }
public ICollection<Address> zAddress { get; set; }
public ICollection<Phone> zPhone { get; set; }
public ICollection<Email> zEmail { get; set; }
public ConstituentPhone zConstituentPhone { get; set; }
public ConstituentEmail zConstituentEmail { get; set; }
public ConstituentAddress zConstituentAddress { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用CRM.DomainClasses;
命名空间CodeFirstShenanigansWeb.Models
{
公共类ComponentListDetailsView
{
公共成分构成{get;set;}
公共ICollection zAddress{get;set;}
公共ICollection zPhone{get;set;}
公共ICollection zEmail{get;set;}
公共宪法电话Z宪法电话{get;set;}
public ConstituteMail ZConstituteMail{get;set;}
公共宪法地址ZConstituteAddress{get;set;}
}
}
谢谢你的帮助。谢谢大家! 您的代码看起来是正确的。您确定此查询返回的是一行,并且您没有可能会提供额外行的联接吗?另外,您可能只需要四个where条件中的一个。为这种情况创建特殊视图不是更好吗?您将简化LINQ。要清楚,我的SQL Server数据库没有重复项,它似乎是相同信息的不同排列。有两组四个不同的子对象,由于它们不对齐,所以它们相乘。我正在考虑采用儿童行动方法:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CRM.DomainClasses.Enums;
using CRM.DomainClasses;
namespace CodeFirstShenanigansWeb.Models
{
public class ConstituentDetailsView
{
public Constituent zConstituent { get; set; }
public Address zAddress { get; set; }
public Phone zPhone { get; set; }
public Email zEmail { get; set; }
public ConstituentPhone zConstituentPhone { get; set; }
public ConstituentEmail zConstituentEmail { get; set; }
public ConstituentAddress zConstituentAddress { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using CRM.DomainClasses;
namespace CodeFirstShenanigansWeb.Models
{
public class ConstituentListDetailsView
{
public Constituent zConstituent { get; set; }
public ICollection<Address> zAddress { get; set; }
public ICollection<Phone> zPhone { get; set; }
public ICollection<Email> zEmail { get; set; }
public ConstituentPhone zConstituentPhone { get; set; }
public ConstituentEmail zConstituentEmail { get; set; }
public ConstituentAddress zConstituentAddress { get; set; }
}