如何编写 Func返回拥有最多孩子的实体?

发布于 2024-09-13 09:01:42 字数 785 浏览 5 评论 0原文

我有一个访问我的实体框架的存储库。我有一个如下所示的方法:

public TEntity FindOne(Expression<Func<TEntity, bool>> criteria)
{
   var query = _queryAll.Where(criteria);
   return query.FirstOrDefault();
}

我有 2 个具有一对多关系的实体。我们称它们为课程学生。一门课程可以有多个学生。我想编写一个查询来返回拥有最多学生的课程。

Courses.OrderByDescending(x=>x.Students.Count()).FirstOrDefault();

但我该如何将其写为 Func 呢?

我希望不是

(x=>x.OrderBy(y=>y.Students.Count()).FirstOrDefault().id == x.id)

因为添加另一个标准看起来不起作用:

    (x=>x.OrderBy(y=>y.Students.Count())
      .FirstOrDefault().id == x.id 
        && x.CourseName == "CS101")

I have a repository accessing my Entity Framework. I have a method that looks like this:

public TEntity FindOne(Expression<Func<TEntity, bool>> criteria)
{
   var query = _queryAll.Where(criteria);
   return query.FirstOrDefault();
}

I have 2 Entities that have a one to many relation. Lets call them Courses and Students. A Course can have multiple Students. I'd like to write a query that returns the Course that has the most students.

Courses.OrderByDescending(x=>x.Students.Count()).FirstOrDefault();

But how would I write that as a Func<T, bool> ?

I hope it's not

(x=>x.OrderBy(y=>y.Students.Count()).FirstOrDefault().id == x.id)

Because adding another criteria looks like it wouldn't work:

    (x=>x.OrderBy(y=>y.Students.Count())
      .FirstOrDefault().id == x.id 
        && x.CourseName == "CS101")

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

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

发布评论

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

评论(1

清眉祭 2024-09-20 09:01:42

根本不清楚为什么你要把 && x.Course == "CS101" 最后。这到底是什么意思?我强烈怀疑问题出在您试图将所有内容都塞进 FindOne 方法中的单个 Where 子句的设计中。很简单,并非所有事情都可以轻松地以这种方式表达。

另外,您应该使用OrderByDescending,以便第一个结果包含最多的学生。

除此之外,就整体查询而言,使用排序对我来说似乎相当合理 - 但它不会完全适合您的 FindOne 方案。

编辑:这是一个比只需要两个条件更大的问题。您可以写:

x => x.CourseId == "CS101" && 
     x.Students.Count() == Courses.Where(x => x.CourseId == "CS101"))
                                  .Max(x => x.Students.Count())

...但问题是,无论如何,您都必须能够访问课程,我想您正在尝试这样做远离。除此之外,它应该可以工作,但它很奇怪。

如果您可以访问课程,您不妨忽略FindOne并自己编写查询:

var course = courses.Where(course => course.CourseId == "CS101")
                    .OrderByDescending(course => course.Students.Count())
                    .FirstOrDefault();

这显然比第一个版本更简单。

It's not at all clear why you'd put the && x.Course == "CS101" at the end. What is that even meant to mean? I strongly suspect the problem is in your design of trying to cram everything into a single Where clause in your FindOne method. Quite simply, not everything can easily be expressed that way.

Also, you should be using OrderByDescending so that the first result has the most students.

Other than that, using ordering seems fairly reasonable to me, in terms of the overall query - but it won't fit neatly into your FindOne scheme.

EDIT: This is a bigger problem than just requiring two conditions. You could write:

x => x.CourseId == "CS101" && 
     x.Students.Count() == Courses.Where(x => x.CourseId == "CS101"))
                                  .Max(x => x.Students.Count())

... but the problem is that at that point you've got to have access to Courses anyway, which I'd imagine you're trying to get away from. Aside from that, it should work but it's grotesque.

If you can get access to courses, you might as well ignore FindOne and write the query yourself:

var course = courses.Where(course => course.CourseId == "CS101")
                    .OrderByDescending(course => course.Students.Count())
                    .FirstOrDefault();

That's clearly simpler than the first version.

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