Public class Class1
{
Public int Id {get; set;}
Public int code {get; set;}
Public int item1 {get; set;}
Public int item2 {get; set;}
Public string data {get; set;}
}
//List filled here
List<Class1> MyList = GetData();
int[] codes = {1, 2, 3};
IEnumerable<Class1> groupItems = new List<Class1>();
//Grouping and ordering logic here
foreach(int code in codes)
{
groupItems = MyList.GroupBy(g => g.Id)
.Where(w => w.Any(a => a.Code = code)
.Select(s => new
{ key = s.Key,
items = s.OrderBy(o => o.Item1)
.ThenBy(t => t.Item2)
}
)
.OrderBy(o => o.Key)
.SelectMany(sm => sm.Items);
}
MyList.Clear();
MyList.InsertRange(0, groupItems.ToList());
从上面的代码输出中,MyList在结果中是空的。我在这里做错了什么?IEnumerable to List,不返回数据
在groupItems上它是Ienumerable并添加到MyList。我使用ToList()转换为List。
快速一瞥,这可能是因为您使用'a.Code = code'作为一个=而不是'=='('a.Code == code') –
@ stephen.vakil:其实,那应该根本不用编译,原因很多。 – SLaks
@Slaks:我的代码或问题中的代码?我在编译的问题中假定了代码,因为问题是,它为什么是空的,而不是,为什么不编译。 –