我有两个主要的域类。每个域类代表一个表。这两个域类具有MANY-TO-MANY关系。所以我创建了另一个域类,为两个域类创建了一对一的关系。域类是:
public class Child
{
public Child()
{
this.UserLog = new UserLog();
this.ChildCares = new List<ChildCare>();
}
public Guid ChildId { get; set; }
public string Surname { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public DateTime DateOfBirth { get; set; }
public string BirthCountry { get; set; }
public string CitizenCountry { get; set; }
public string FatherSurname { get; set; }
public string FatherFirstName { get; set; }
public string FatherMiddleName { get; set; }
public string MotherSurname { get; set; }
public string MotherFirstName { get; set; }
public string MotherMiddleName { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public virtual UserLog UserLog { get; private set; }
public virtual ICollection<ChildCare> ChildCares { get; private set; }
public class Institute
{
public Institute()
{
this.UserLog = new UserLog();
this.ChildCares = new List<ChildCare>();
}
public Guid InstituteId { get; set; }
public string InstituteName { get; set; }
public int Capacity { get; set; }
public decimal AmountPerDay { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Telephone { get; set; }
public string Mobile { get; set; }
public UserLog UserLog { get; set; }
public virtual ICollection<ChildCare> ChildCares { get; private set; }
}
public class ChildCare
{
public ChildCare()
{
this.UserLog = new UserLog();
//this.Children = new Child();
//this.Institute = new Institute();
}
public Guid ChildCareId { get; set; }
public Guid ChildId { get; set; }
public Guid InstituteId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public UserLog UserLog { get; set; }
public virtual Child Children { get; set; }
public virtual Institute Institute { get; set; }
Child表有三行。 Institute表有SIX行。 ChildCare表有三行。不,我想通过加入Child,Institute,ChildCare来选择数据。由于ChildCare类同时具有Child和Institute对象。所以我想从ChildCare类查询。我希望Linq查询将数据作为以下对象的集合返回,这是另一个域类。
public class ChildDayCare
{
public string ChildrenName { get; set; }
public string InstituteName { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public decimal ChargePerDay { get; set; }
}
现在我写的查询如下:
public IList<ChildCare> GetAllChildrenCareAsList()
{
try
{
var childCareList = base.GetAll();
var childCareArr = childCareList as ChildCare[] ?? childCareList.ToArray();
return childCareArr.Any() ? childCareArr.ToList() : null;
}
catch (Exception ex)
{
throw ex;
}
}
var childCares = GetAllChildrenCareAsList();
if (childCares != null)
{
IList<Child> children = childCares.Select(t => t.Children).ToList();
IList<Institute> institutes = childCares.Select(t => t.Institute).ToList();
var childDacareList = (from cc in childCares
join cd in children on cc.Children.ChildId equals cd.ChildId
join ins in institutes on cc.Institute.InstituteId equals ins.InstituteId
select new ChildDayCare
{
ChildrenName = cd.MiddleName, //cd.FirstName + " " + cd.MiddleName + " " + cd.Surname,
InstituteName = ins.InstituteName,
StartDate = cc.StartDate,
EndDate = cc.EndDate,
ChargePerDay = ins.AmountPerDay
}).ToList();
return childDacareList;
}
查询应显示三行。但相反它显示9行。谁能告诉我我做错了什么。
答案 0 :(得分:0)
以下代码将有所帮助: IList institutes = children.Select(t =&gt; t.Institute)。ToList();