gpt4 book ai didi

c# - 基于关联属性聚合的约束查询

转载 作者:太空宇宙 更新时间:2023-11-03 16:53:08 26 4
gpt4 key购买 nike

小问题:这会为每个标签项触发一个查询吗?

public static IQueryable<Tag> WhereTagHasLivePosts(this IQueryable<Tag> q)
{
return q.Where(t => t.Posts.Where(p => DateTime.Now >= p.PublishTime && p.IsPublished == true).Count() > 0);
}

t.Posts.Where 实际上是 IEnumerable 的扩展,而不是 IQueryable,所以这似乎不是一个好主意。

提前致谢

最佳答案

您完全正确 - t.Posts 是一个 EntityCollection(在 Linq-to-SQL 中,它将是一个 EntitySet)并因此实现 IEnumerable 而不是 IQueryable

虽然线程是关于 Linq-to-SQL 的,但它的逻辑与此相同:

http://damieng.com/blog/2009/06/01/linq-to-sql-changes-in-net-40

迈克恩说:

[...] EntitySet which is NOT IQueryable.

This prevents usage of linq2sql in any real life scenario.

Consider:

client.Orders.Where(o=>o.Total>1000); // runs linq to objects rather than linq to sql

Same problem exists in entiy framework.

达米安回应:

@Mikeon: EntitySet is the loaded in-memory collection for relationships.

If you want all the orders for a specific client where the total is greater than 100 then you perform:

db.Orders.Where(o => o.Client == client && o.Total > 1000)

关于c# - 基于关联属性聚合的约束查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3234342/

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