假设我有一个如下列表:
List <Customclass> someList = new List<CustomClass>()
{
new CustomClass { color = "Red", type = "lcd" },
new CustomClass { color = "Red", type = "cvr" },
new CustomClass { color = "Green", type = "lcd" },
new Customclass { color = "Green", type = "cvr" },
new CustomClass { color = "Blue", type = "lcd" },
new CustomClass { color = "Blue", type = "cvr" }
};
如何过滤列表以仅显示color
值且type
必须为lcd
SELECT [color] FROM [someList] WHERE [type] = 'lcd';
,输出为:
Red
Green
Blue
我对LINQ很新,因为我没有暗示如何做这件事。所以我希望有人或任何人可以帮助我。谢谢
答案 0 :(得分:3)
您需要以下内容:
var colors = someList.Where(cc=>cc.type=="lcd")
.Select(cc=>cc.color);
Where
方法和谓词来过滤您的列表,该谓词要求输出序列中的项目应具有lcd
类型。Select
方法进行投影,并仅选择已过滤序列中每个项目的color
。答案 1 :(得分:0)
简单,
someList.Where(x=> x.type == "lcd").Select(x=> x.color).ToList();
或者
var results = (from x in someList
where x.color == "lcd"
select x.color).ToList();