如何解决如何使用linq从具有最新dateTime的集合中获取整个记录?
我有一个实体,该实体具有许多列,并且具有一对多关系中另一个实体的导航属性,现在在我的导航属性中,我具有一个col dateTimeStamp,现在我想获取包括我的navi属性记录的记录,但仅此具有最新的dateTimeStamp。我们如何使用Lambda表达式linq查询来做到这一点?
实体看起来像
public class EntityA {
int entityAId {get;set;}
string desc {get;set;}
...
<ICollection<EntityB>> EntitiesB {get;set;}
}
public class EntityB {
int entityAId {get;set;}
int entityBId {get;set;}
dateTime dateTimeStamp {get;set;}
...
EntityA EntityANavi {get;set;}
}
}
因此每个EntityA都有许多EntityB,但是我只想要具有最新dateTime的EntityB记录,如何使用lambda表达式和linq运算符来做到这一点?
数据看起来像:-
EntityA
EntityAId Desc ...
12 sample1
22 sample2
EntityB
EntityBId EntityAId DateTimeStamp ....
1 12 2020-04-01-10:10:01
1 12 2020-04-01-10:10:21
1 12 2020-04-01-10:11:01
2 22 2020-04-01-11:10:51
2 22 2020-04-01-11:11:01
结果:一个对象
EntityA :{
{entityAId : 12,desc: sample1
...
EntitiesB :{
EntityB:{ EntityAId:12,EntityBId :1,DateTimeStamp :2020-04-01-10:11:01 .... }
},{entityAId : 22,desc: sample2
...
EntitiesB :{
EntityB:{ EntityAId:22,EntityBId :2,DateTimeStamp :2020-04-01-11:11:01 .... }
}
}
}
我尝试使用EntityA.Include( a => a.EntitiesB.orderByDescending(b => b.DateTimeStamp).First() )
但我不断出错
解决方法
我假设您正在使用实体框架。 对于EFcore来说,好消息是,它将在未来推出,并且在efcore的预览版5.0中可用:https://github.com/dotnet/efcore/commit/21b9a35db594f7a383e855f922babbe54b3d38c5
就目前而言,在一个急切的加载方案中,只要您包括您要加载的对象,就包括它。您可以为EntityB加载所需的实体,并将EntityA包括在其中。 一个可行的解决方案是:
List<EntityA> listA = ctx.EntityA.Include(k=>k.EntitiesB).ThenInclude(k=>k.EntityANavi)
.select(k=>k.EntitiesB.OrderByDescending(l=>l.DateTimeStamp).FirstOrDefualt())
.ToList().Where(EntityANavi != null).Select(k=>k.EntityANavi);
基本上可以加载所有内容,然后选择所需的那些对象。在包含EntitiesB之后,我再次包含EvntityANavi以确保保留该关系。之后,只需选择“过滤器”,然后忘记。 (.ToList()实际上是加载数据,并确保您只为datareader运行一个查询,因为我们一直在进行查询)
,我想获取包括我的navi属性记录在内的记录,但仅包含具有最新dateTimeStamp的记录。
var entityAWithItsNewestEntityB = dbContext.EntitiesA.Select(entityA => new
{
// Select the EntityA properties that you plan to use:
Id = entityA.Id,Name = entityA.Name,...
NewestEntityB = entityA.EntitiesB
.OrderByDescending(entityB => entityB.DateTimestamp)
.Select(entityB => new
{
// Select only the EntityB properties that you plan to use
Id = entityB.Id,Name = entityB.Name,...
// Foreign key not needed,you already know the value:
// EntityeAId = entityB.EntityAId,})
.FirstOrDefault(),});
换句话说:从EntityA对象集合中的每个EntityA对象中,选择一个或多个属性。还要通过降序DateTimeStamp排序此EntityA的所有EntityB对象,这样最新的EntityB将排在第一位。从EntityB对象的此有序序列中,选择几个属性。从结果中仅获取第一个,如果EntityA根本没有EntityB对象,则为null。
简单的漫画卓悦!
,尝试:
def calculate_quotient_and_remainder(y,d):
quotient = []
remainder = []
for i in y:
quotient.append(i//d)
remainder.append(i%d)
print('Quotient: {}'.format(quotient))
print('Remainder: {}'.format(remainder))
test_case1 = [20,7,88,66,18]
test_case2 = [73,57,67,61,83]
test_case3 = [14,42,45,35,33]
test_case4 = [77,31,44,12,11]
test_case5 = [22,80,16,84]
dividend_1 = [9]
dividend_2 = [9]
dividend_3 = [4]
dividend_4 = [8]
dividend_5 = [4]
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。