QQ:由poco集团不工作 [英] QQ: Group by poco not working
问题描述
我有一个产品
数组,一个自定义POCO。其中一些产品指的是同一个产品。一些将具有TariffUnrestricted价格,另一个将具有StandingChargePenceDay。我需要将它们合并在一起。
我正在尝试将产品组合在一起,然后选择每个产品的密钥和值。我认为代码会更好地解释:
Hi,
I have an array of Products
, a custom POCO. Some of these products refer to the same item. Some will have the TariffUnrestricted price and another will have the StandingChargePenceDay . I need to merge these together.
I am trying to group the products together, then select out the key and the values for each product. I think the code will explain better:
public override Product[] Import(string path, Func<Worksheet, bool> worksheetSelector = null)
{
Product[] products = base.Import(path, worksheetSelector);
var groups = products.GroupBy(p => new Product
{
PlanName = p.PlanName,
RateType = p.RateType,
ContractDuration = p.ContractDuration,
Duration = p.Duration,
TariffDescription = p.TariffDescription,
//TariffUnrestricted = p.TariffUnrestricted,
//StandingChargePenceDay = p.StandingChargePenceDay
});
products = groups.Select(g =>
{
Product product = g.Key;
product.TariffUnrestricted = g.Max(p => p.TariffUnrestricted);
product.StandingChargePenceDay = g.Max(p => p.StandingChargePenceDay);
return product;
}).ToArray();
return products;
}
物品不合并。组的数量始终与产品数量相同(1:1)。
我尝试制作产品IComparable< Product> ;,IComparer< Product> ;, IEquatable<产品与GT;但是什么都没有用?
我的google-fu是一周。我找不到正确的用语:C
我做错了什么?
谢谢
Andy
The items do not merge. The number of groups is always the same as the number of products (1:1).
I tried making the Product IComparable<Product>, IComparer<Product>, IEquatable<Product> but nothing works?
My google-fu is week. I cannot find the correct terms :C
What am I doing wrong?
Thanks
Andy
推荐答案
为什么要尝试按新产品分组?所有这一切都是尝试按参考值本身进行分组,不是吗?这将解释组和产品之间的1:1相关性。 :笑:
你不想要这样的东西:
Why are you trying to group by a new product? All that will do is attempt to group by the reference value itself, won't it? Which would explain the 1:1 correlation between groups and products. :laugh:
Don't you want something like:
var groups = products.GroupBy(p => p.TariffDescription);
或类似的?
[edit]
快速测试:
or similar?
[edit]
A quick test:
public class MyClass
{
public int Value { get; set; }
public string Name { get; set; }
}
和
And
List<MyClass> list = new List<MyClass>();
MyClass a = new MyClass() { Value = 1, Name = "A" };
MyClass b = new MyClass() { Value = 1, Name = "B" };
MyClass c = new MyClass() { Value = 2, Name = "C" };
MyClass d = new MyClass() { Value = 2, Name = "D" };
MyClass e = new MyClass() { Value = 3, Name = "E" };
list.Add(a);
list.Add(b);
list.Add(c);
list.Add(d);
list.Add(e);
var g1 = list.GroupBy(m => m.Value);
var g2 = list.GroupBy(m => new MyClass { Value = m.Value, Name = m.Name });
Console.WriteLine(g1.Count() + ":" + g2.Count());
给出3:5
添加IEquatable接口不会改变:
Gives "3:5"
Adding the IEquatable interface doesn't change that either:
public class MyClass : IEquatable<MyClass>
{
public int Value { get; set; }
public string Name { get; set; }
public bool Equals(MyClass other)
{
return Value == other.Value;
}
}
我仍然得到3:5
[/ edit]
I still get 3:5
[/edit]
>
这篇关于QQ:由poco集团不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!