将 VB Linq 转换为 C#

发布于 2024-11-01 01:42:26 字数 1128 浏览 1 评论 0原文

我正在通过转换现有项目自学 C#,并且一直在转换以下 vb linq 代码:

Dim outStuff = From tt In (From t In Products.SelectMany(Function(p) If(p.tags IsNot Nothing, p.tags, New ObservableCollection(Of TagModel)))
               Group By tagName = t.name,
                                  v = (Aggregate p In Products Where If(p.tags IsNot Nothing, p.tags.Contains(t), Nothing) Into Sum(p.views)),
                                  nl = (Aggregate p In Products Where If(p.tags IsNot Nothing, p.tags.Contains(t), Nothing) Into Sum(p.num_likes))
               Into g = Group, Count())
               Group By name = tt.tagName Into Count = Sum(tt.Count), viewsTotal = Sum(tt.v), num_likesTotal = Sum(tt.nl)
               Select name, Count, viewsTotal, num_likesTotal

其中 Products As ObservableCollection(Of ProductModel)

到目前为止我已经成功转换了这么多:

var x =  Products.SelectMany(p => (p.tags != null) ? p.tags : new ObservableCollection<TagModel>());
var tags = from t in x group t by t.name into g select new { tagname=g.First().name};

“Group By”让我难住了。任何帮助都会很棒...

I'm in the process of teaching myself C# by converting an existing project and am stuck converting the following vb linq code:

Dim outStuff = From tt In (From t In Products.SelectMany(Function(p) If(p.tags IsNot Nothing, p.tags, New ObservableCollection(Of TagModel)))
               Group By tagName = t.name,
                                  v = (Aggregate p In Products Where If(p.tags IsNot Nothing, p.tags.Contains(t), Nothing) Into Sum(p.views)),
                                  nl = (Aggregate p In Products Where If(p.tags IsNot Nothing, p.tags.Contains(t), Nothing) Into Sum(p.num_likes))
               Into g = Group, Count())
               Group By name = tt.tagName Into Count = Sum(tt.Count), viewsTotal = Sum(tt.v), num_likesTotal = Sum(tt.nl)
               Select name, Count, viewsTotal, num_likesTotal

where Products As ObservableCollection(Of ProductModel)

I've mananged to convert this much so far:

var x =  Products.SelectMany(p => (p.tags != null) ? p.tags : new ObservableCollection<TagModel>());
var tags = from t in x group t by t.name into g select new { tagname=g.First().name};

The 'Group By's has me stumped. Any help would be great...

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

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

发布评论

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

评论(2

救赎№ 2024-11-08 01:42:26

您的查询有点复杂且难以理解,但让我尝试描述一下我认为您正在寻找的内容。您有一个产品列表,每个产品可能有一个或多个标签;您需要所有标签的列表,以及具有该标签的产品数量、具有该标签的产品的总浏览次数以及具有该标签的产品的“喜欢”总数。如果是这种情况,下面的方法应该可以解决问题:

// may want to add ToArray() here so that filter is not executed multiple times during subsequent query
var productsWithTags = Products.Where(p => p.tags != null);
var outStuff = from t in (from p in productsWithTags
               from t in p.tags
               select t).Distinct()
               let matchingProducts = productsWithTags.Where(p => p.tags.Contains(t))
               select new { name = t.name,
                            Count = matchingProducts.Count(),
                            viewsTotal = matchingProducts.Sum(p => p.views),  
                            num_likesTotal = matchingProducts.Sum(p => p.num_likes)
                          };

Your query is a little convoluted and hard to follow, but let me try to describe what I think you are looking for. You have a list of Products, each of which may have one or more tags; and you want a list of all of the tags, with the count of how many products have that tag, the total number of views of products with that tag, and the total number of "likes" of the product with that tag. If that is the case, the following should do the trick:

// may want to add ToArray() here so that filter is not executed multiple times during subsequent query
var productsWithTags = Products.Where(p => p.tags != null);
var outStuff = from t in (from p in productsWithTags
               from t in p.tags
               select t).Distinct()
               let matchingProducts = productsWithTags.Where(p => p.tags.Contains(t))
               select new { name = t.name,
                            Count = matchingProducts.Count(),
                            viewsTotal = matchingProducts.Sum(p => p.views),  
                            num_likesTotal = matchingProducts.Sum(p => p.num_likes)
                          };
浮世清欢 2024-11-08 01:42:26

你的代码看起来有点疯狂:) ...很难理解你真正想要的是什么,但我认为就是这样:

var outStuff = Products.SelectMany(p => p.tags)
    .Where(t => t != null)
    .GroupBy(t => t.name)
    .Select(g => new
    {
        Name = g.Key,
        Count = g.Sum(),
        ViewsTotal = g.Sum(x => x.v),
        LikesTotal = g.Sum(x => x.nl),
    });

Your code looks a little crazy :) ... it's hard to understand what you actually want, but I think this is it:

var outStuff = Products.SelectMany(p => p.tags)
    .Where(t => t != null)
    .GroupBy(t => t.name)
    .Select(g => new
    {
        Name = g.Key,
        Count = g.Sum(),
        ViewsTotal = g.Sum(x => x.v),
        LikesTotal = g.Sum(x => x.nl),
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文