繁体   English   中英

LInq to SQl问题…请帮助

[英]LInq to SQl problem…Please help

我有以下数据

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

我想要以下结果

12  test 0.0 / test8.8/test 7.9
27  asdf 9.0/asdf 2.0/ asdf 2.0

到目前为止,我只能提出这个建议..有人可以指出我正确的方向吗

非常感谢

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()),
                                 };
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()));
}

更好的做法是按照以下方式提供Product.ToString的实现:

public override ToString() {
    return String.Format("{0}, {1:0.0}", this.Description, this.Cost);
}

并将上面的Console.WriteLine替换为

Console.WriteLine(String.Join("/", group.Select(p => p.ToString()).ToArray()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM