将 VB Linq 转换为 C#
我正在通过转换现有项目自学 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的查询有点复杂且难以理解,但让我尝试描述一下我认为您正在寻找的内容。您有一个产品列表,每个产品可能有一个或多个标签;您需要所有标签的列表,以及具有该标签的产品数量、具有该标签的产品的总浏览次数以及具有该标签的产品的“喜欢”总数。如果是这种情况,下面的方法应该可以解决问题:
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:
你的代码看起来有点疯狂:) ...很难理解你真正想要的是什么,但我认为就是这样:
Your code looks a little crazy :) ... it's hard to understand what you actually want, but I think this is it: