C# 每个根节点需要返回一个实体的查询的LinqToEntities代码?
我想这件事把我自己弄糊涂了。我的数据库看起来像:C# 每个根节点需要返回一个实体的查询的LinqToEntities代码?,c#,visual-studio-2010,linq-to-entities,relational-database,domainservices,C#,Visual Studio 2010,Linq To Entities,Relational Database,Domainservices,我想这件事把我自己弄糊涂了。我的数据库看起来像: Locations [root] Inspections [level1] InspectionItems [level2] InspectionReadings [level3] Areas [level1] Inspections [level2] InspectionItems [level3] Inspecti
Locations [root]
Inspections [level1]
InspectionItems [level2]
InspectionReadings [level3]
Areas [level1]
Inspections [level2]
InspectionItems [level3]
InspectionReadings [level4]
每个表都通过Guid链接到上一个表,并且检查表具有可为空的AreaID,因此它可以是位置的直接子级
我需要的是
for each Location
{take all InspectionReadings entities
where location == location
sort date descending
return the top Entity details}
add the Location details and the InspectionReading details into a new table
return new table
结果应为带有位置列表及其最新检查读取日期的数据网格。每个位置只应出现一次
我管理的是(这在我的DomainService.cs中)
public IQueryable GetLocationStatus()
{
var status=(来自ObjectContext.Locations中的
从ObjectContext.InspectionReads中的b
orderby b.日期降序,a.名称
选择新位置状态列表()
{
ID=b.ID,
LocationName=a.名称,
LastInspectionDate=b.DateTake??DateTime.MinValue,//数据可为空,因此在这种情况下需要返回一个值
StatusNumber=b。Status???-1//数据可为空,因此在这种情况下需要返回一个值
});
返回状态;
}
它返回整个InspectionItems实体及其相关位置,尽管我已经尝试过,但我找不到一种方法来做我需要的事情
我想在DomainService类中完成这方面的所有代码,但目前我唯一能想到的方法是将查询的每个位置名称作为viewmodel中的一个参数,返回一个实体,创建一个新列表并添加每个实体
当然,这些都可以在LINQ查询中完成?最终版本,如果我没有弄错的话:
var status = (from a in ObjectContext.Locations
select new {Location = a, LastReading = a.Inspections.SelectMany(i=>i.InspectionReadings).OrderBy(r=>r.PostDate).FirstOrDefault()};
好的,试试这个:
var result = from l in Locations
from i in l.Inspestions
from ii in i.InspestionItems
from ir in ii.InspectionReadings
group ir by l into g
select new {Location = g.Key, LastReading = g.OrderBy(r=>r.DateTaken).LastOrDefault() };
Last或default,因为默认排序顺序是升序,您需要最新的(Last)。谢谢@Val,我现在的代码是:
public IQueryable<LocationStatusList> GetLocationStatus()
{
var status = (from a in ObjectContext.Locations
select new LocationStatusList
{
ID=a.ID,
Name=a.Name,
LastInspectionDate= (from b in ObjectContext.Inspections
from c in ObjectContext.InspectionReadings
where c.InspectionItemID==b.ID && b.LocationID==a.ID
orderby c.DateTaken descending
select c.DateTaken).FirstOrDefault()??DateTime.MinValue,
StatusNumber = (from b in ObjectContext.Inspections
from c in ObjectContext.InspectionReadings
where c.InspectionItemID==b.ID && b.LocationID==a.ID
orderby c.DateTaken descending
select c.Status).FirstOrDefault()??-1,
});
return status;
}
public IQueryable GetLocationStatus()
{
var status=(来自ObjectContext.Locations中的
选择新位置状态列表
{
ID=a.ID,
Name=a.Name,
LastInspectionDate=(来自ObjectContext.Inspections中的b
从ObjectContext.InspectionReads中的c
其中c.InspectionItemID==b.ID&&b.LocationID==a.ID
orderby c.datetake降序
选择c.datetake).FirstOrDefault()??DateTime.MinValue,
StatusNumber=(来自ObjectContext.Inspections中的b
从ObjectContext.InspectionReads中的c
其中c.InspectionItemID==b.ID&&b.LocationID==a.ID
orderby c.datetake降序
选择c.Status).FirstOrDefault()??-1,
});
返回状态;
}
这正是我所需要的。谢谢大家! 谢谢!我可以看到这将如何工作,不幸的是,我的InspectionReads表不是我的Inspections表的直接子表,因此它会在上抛出一个错误。选择Many,因为它找不到键关系。InspectionReads有一个InspectionItemsID,InspectionItems有一个InspectionID,所以在层次结构中有一个额外的级别。我需要一些类似的东西:LastReading=a.Inspections.SelectMany(I=>I.InspectionItems.SelectMany(j=>j.InspectionReads.OrderBy(r=>r.DateTake.FirstOrDefault())您能帮忙吗?它会抛出一个错误“方法的类型参数”System.Linq.Enumerable.SelectMany(System.Collections.Generic.IEnumerable,System.Func)无法从用法中推断。尝试显式指定类型参数。'确定尝试:`从l中的i位置的l。从i中的ii中的Inspestions从i中的ii中的InspestionItems从ii中的ir中的InspestionReadings我已经找到了解决方案!!!非常感谢你的帮助——它需要一点技巧,但现在它正是我想要的。我会把它作为答案,但你会因为给了我我需要的那一点而得到一张赞成票!
public IQueryable<LocationStatusList> GetLocationStatus()
{
var status = (from a in ObjectContext.Locations
select new LocationStatusList
{
ID=a.ID,
Name=a.Name,
LastInspectionDate= (from b in ObjectContext.Inspections
from c in ObjectContext.InspectionReadings
where c.InspectionItemID==b.ID && b.LocationID==a.ID
orderby c.DateTaken descending
select c.DateTaken).FirstOrDefault()??DateTime.MinValue,
StatusNumber = (from b in ObjectContext.Inspections
from c in ObjectContext.InspectionReadings
where c.InspectionItemID==b.ID && b.LocationID==a.ID
orderby c.DateTaken descending
select c.Status).FirstOrDefault()??-1,
});
return status;
}