LINQ 哪里计数检查

发布于 2024-12-01 17:11:49 字数 985 浏览 1 评论 0原文

所以我慢慢地了解 LINQ 语法,但是这个查询让我发疯..

我只想返回具有多个过滤器值的过滤器..我尝试在 fv 上放置一个 group by 但 p 超出了那么范围..

        var filters = (from p in _db.Products
                  join pf in _db.ProductFilterSets on p.ProductID equals pf.ProductID
                  join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID
                  join f in _db.Filters on fv.FilterID equals f.FilterID
                  where products.Contains(p) 
                  select f).Distinct();

我做错了什么?

我理想地需要:

        var filters = (from p in _db.Products
                  join pf in _db.ProductFilterSets on p.ProductID equals pf.ProductID
                  join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID
                  join f in _db.Filters on fv.FilterID equals f.FilterID
                  where products.Contains(p) && fv.Count() > 1
                  select f).Distinct();

谢谢

So I'm slowly getting my head around the LINQ syntax, but this query's driving me nuts..

I only want to return filters with more than one filter value.. I've tried putting a group by on fv but p goes out of scope then..

        var filters = (from p in _db.Products
                  join pf in _db.ProductFilterSets on p.ProductID equals pf.ProductID
                  join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID
                  join f in _db.Filters on fv.FilterID equals f.FilterID
                  where products.Contains(p) 
                  select f).Distinct();

What am I doing wrong?

I ideally need:

        var filters = (from p in _db.Products
                  join pf in _db.ProductFilterSets on p.ProductID equals pf.ProductID
                  join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID
                  join f in _db.Filters on fv.FilterID equals f.FilterID
                  where products.Contains(p) && fv.Count() > 1
                  select f).Distinct();

Thank you

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

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

发布评论

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

评论(1

你的呼吸 2024-12-08 17:11:49

您可以像这样编写查询,在 _db.FilterValues 上添加另一个联接,但使用 into 关键字将结果投影到一个组

var filters = (from p in _db.Products
               join pf in _db.ProductFilterSets on p.ProductID equals pf.ProductID
               join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID into fvg
               join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID
               join f in _db.Filters on fv.FilterID equals f.FilterID
               where products.Contains(p) && fvg.Count() > 1
               select f).Distinct();

You can write the query like this, adding another join on the _db.FilterValues but projecting the result to a group by using the into keyword

var filters = (from p in _db.Products
               join pf in _db.ProductFilterSets on p.ProductID equals pf.ProductID
               join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID into fvg
               join fv in _db.FilterValues on pf.FilterValueID equals fv.FilterValueID
               join f in _db.Filters on fv.FilterID equals f.FilterID
               where products.Contains(p) && fvg.Count() > 1
               select f).Distinct();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文