我只需要从List对象中选择那些段名只有一次的项目,例如在附加的图像中,只返回具有SegmentName =" C"的元素;因为在列表中只有一次...
这是我到目前为止尝试的示例代码。
var empty = count
.Where(x => x.Count > 1 & x.SegmentName.Count() == 1)
.Select(k => new {
SegName = k.SegmentName,
Value = k.FieldValue,
No = k.Count })
.ToList();
样品:
答案 0 :(得分:1)
这可能会为你做到这一点
elements.GroupBy(x => x).Where(y => y.Count() <= 1).Select(z => z.Key).ToList();
这将返回仅存在一次的所有元素的列表
示例
List<string> SegmentName = new List<string>();
SegmentName.Add("A");
SegmentName.Add("A");
SegmentName.Add("B");
SegmentName.Add("B");
SegmentName.Add("C");
List<string> newlist = SegmentName.GroupBy(x => x).Where(g => g.Count() <= 1).Select(y => y.Key).ToList();
输出
&#34; C&#34;
因此,对于您的自定义对象,它应该是
count.GroupBy(x => x.SegmentName)
.Where(y=> y.Count()<=1)
.Select(k => new {
SegName = k.SegmentName,
Value = k.FieldValue,
Count = k.Count
})
.ToList()
答案 1 :(得分:1)
希望您正在寻找类似的东西:
array[i][j]
答案 2 :(得分:0)
给GroupBy
一个
var empty = count.GroupBy(x => x.SegmentName)
.Select(x => x.First())
.ToList();
答案 3 :(得分:0)
答案 4 :(得分:0)
var count = new [] {
new { SegmentName = "A", FieldValue = "", Count = 2 },
new { SegmentName = "A", FieldValue = "ABC", Count = 1 },
new { SegmentName = "B", FieldValue = "", Count = 2 },
new { SegmentName = "B", FieldValue = "ABC", Count = 1 },
new { SegmentName = "C", FieldValue = "", Count = 3 },
};
var result = count
.GroupBy(x => x.SegmentName)
.Where(y => y.Count() == 1)
.Select(z => z.First()) // Need to implicitly get the first item here
.ToList();
foreach(var item in result)
{
Console.WriteLine("SegmentName={0} FieldValue={1} Count={2}", item.SegmentName, item.FieldValue, item.Count);
}