Reputation:
I have the following data
ProductId Description cost
12 test 0.0
12 test 8.8
12 test 7.9
27 asdf 9.0
27 asdf 2.0
27 asdf 2.0
I want the following result
12 test 0.0 / test8.8/test 7.9
27 asdf 9.0/asdf 2.0/ asdf 2.0
so far i could only come up with this ..can someone point me in the right direction
Thank you so much
var n = from c in query
group new {c}
by new
{
c.productid,
c.cost,
c.prodescription
}
into g
select new{
g.Key.productid,
Products=(g.Key.prodescription) + g.Key.cost.ToString()),
};
Upvotes: 0
Views: 70
Reputation: 241789
var groups = from result in query
group result by new { result.ProductId } into g
select g;
foreach(var group in groups) {
Console.Write("{0}: ", group.Key.ProductId);
Console.WriteLine(String.Join("/", group.Select(p => String.Format("{0}, {1:0.0}", p.Description, p.Cost)).ToArray()));
}
Even better would be to provide an implementation of Product.ToString
along the lines of
public override ToString() {
return String.Format("{0}, {1:0.0}", this.Description, this.Cost);
}
and replace the Console.WriteLine
above by
Console.WriteLine(String.Join("/", group.Select(p => p.ToString()).ToArray()));
Upvotes: 4