如何在 LINQ 查询中执行此聚合、分组依据?

发布于 2024-09-01 15:27:46 字数 965 浏览 1 评论 0原文

请不要给我一个完整的工作示例,我想知道这是如何完成的,而不是获取一些可以复制粘贴的代码

这是我需要的查询,但不能用于我在 LINQ 中创建它。

SELECT * FROM
dbo.Schedules s, dbo.Videos v
WHERE s.VideoID = v.ID
AND s.ID IN 
(
    SELECT MAX(ID) FROM dbo.Schedules
    WHERE ChannelID = 1
    GROUP BY VideoID
)
ORDER BY v.Rating DESC, s.StartTime DESC

我在 LINQ 中有“IN”查询,我认为,它是这样的,

var uniqueList =  from schedule in db.Schedules
                  where schedule.ChannelID == channelID
                  group schedule by schedule.VideoID into s
                  select new
                  {
                      id = s.Max(i => i.ID)
                  };

这可能是错误的,但现在我无法在 where 子句中检查另一个查询 uniqueList.Contains(schedule.ID)

可能有更好的方法来编写此查询,如果您有任何想法,我会喜欢一些提示。

我收到此错误,但它没有多大意义。

无法从用法中推断出方法“System.Linq.Queryable.Contains(System.Linq.IQueryable, TSource)”的类型参数。尝试显式指定类型参数。

Please do not give me a full working example, I want to know how this is done rather than to get some code I can copy paste

This is the query I need, and can't for the life of me create it in LINQ.

SELECT * FROM
dbo.Schedules s, dbo.Videos v
WHERE s.VideoID = v.ID
AND s.ID IN 
(
    SELECT MAX(ID) FROM dbo.Schedules
    WHERE ChannelID = 1
    GROUP BY VideoID
)
ORDER BY v.Rating DESC, s.StartTime DESC

I have the "IN" query in LINQ I think, it's something like this

var uniqueList =  from schedule in db.Schedules
                  where schedule.ChannelID == channelID
                  group schedule by schedule.VideoID into s
                  select new
                  {
                      id = s.Max(i => i.ID)
                  };

It is possibly wrong, but now I can not check in another query for this in a where clause uniqueList.Contains(schedule.ID)

There is possibly a better way to write this query, if you have any idea I would love some hints.

I get this error and it's not making much sense.

The type arguments for method 'System.Linq.Queryable.Contains(System.Linq.IQueryable, TSource)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-09-08 15:27:46

尝试使用 .Where(sInner=> s.ID == sInner.ID).Any()。

我只能在具有简单列表的 linq 表达式中使用 .Contains 。尽管您发布的内容似乎是编译时错误,但一旦您解决了它,我认为您最终会遇到像我过去一样的运行时错误。

try with .Where(sInner=> s.ID == sInner.ID).Any().

I have only been able to use .Contains in linq expressions with simple lists. Although what you posted seems to be a compile time error, once you get around it I think you'll end up getting a runtime error like I have in the past.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文