我有LINQ语句的问题,我有一个颜色列表,这是我的类HSLCOLOR,我保留有关色调,饱和度,亮度和像素坐标的信息 - x和y
public class HSLColor
{
public double H;
public double S;
public double L;
public int x;
public int y;
}
我加载了一个有425600像素的图像,我试图计算多少像素具有相同的亮度, - 这很容易,但是当我这样做时,我松开了关于我的像素的所有其他信息(H,S,x ,y不再可用)
var q = (from x in colors
group x by x.L into g
let count = g.Count()
orderby count descending
select new { Value = g.Key, Count = count}).ToList();
那么,这样做的正确性是什么?要让它们按Lightness排序,还要保留关于其他组件的所有信息吗?
答案 0 :(得分:3)
使用SelectMany
从群组中提取项目:
var q = (from x in colors
group x by x.L into g
let count = g.Count()
orderby count descending
select g)
.SelectMany(g => g)
.ToList();
或者,正如Servy指出的那样,您可以在查询语法中完成所有操作:
var q = (from x in colors
group x by x.L into g
let count = g.Count()
orderby count descending
from c in g
select c)
.ToList();