s.UserId == userI-6ren">
gpt4 book ai didi

c# - 如何按嵌套实体的字段排序/排序?

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:25 27 4
gpt4 key购买 nike

怎么会这样呢?

context.Subscriptions
.Include("TvShow.News")
.Include("User")
.Where(s => s.UserId == userId)
.OrderByDescending(t => t.TvShow.News.CreatedOnDatetime)
.ToList();

OrderByDescending 在这种情况下不起作用,我把它放在那里只是为了更容易理解问题。

最佳答案

我终于让它与这个意大利面条代码一起工作:

// NASTY NASTY ENTITY FRAMEWORK
var anonymousObjects = context.Subscriptions
.Where(c => c.UserId == userId)
.Select(s => new
{
SubscriptionId = s.Id,
TvShowId = s.TvShowId,
TvShow = s.TvShow,
TvShowNews = s.TvShow.News.OrderByDescending(n => n.CreatedOnDatetime),
UserId = s.UserId,
User = s.User
});

anonymousObjects = anonymousObjects.OrderByDescending(x => x.TvShowNews.Max(n => n.CreatedOnDatetime));
var subscriptions = new List<Subscription>();
foreach (var oneAnonymousObject in anonymousObjects)
{
var subscription = new Subscription
{
Id = oneAnonymousObject.SubscriptionId,
TvShowId = oneAnonymousObject.TvShowId,
UserId = oneAnonymousObject.UserId,
TvShow = new TvShow
{
Id = oneAnonymousObject.TvShow.Id,
Title = oneAnonymousObject.TvShow.Title,
CreatedOnDatetime = oneAnonymousObject.TvShow.CreatedOnDatetime,
IsPrivate = oneAnonymousObject.TvShow.IsPrivate,
TotalSubscriptions = oneAnonymousObject.TvShow.TotalSubscriptions,
News = oneAnonymousObject.TvShowNews.ToList()
},
User = oneAnonymousObject.User
};
subscriptions.Add(subscription);
}

Entity Framework 团队,如果你看到这个,请让这更简单。

关于c# - 如何按嵌套实体的字段排序/排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31774642/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com