我该如何改写这个更LINQy? [英] How do I rewrite this to be more LINQy?
本文介绍了我该如何改写这个更LINQy?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这组数据。 Events有属性 EventGroups
,类型列表< Groups>
I have this set of data here. Events has a property EventGroups
that is of type List<Groups>
List<Events> e;
List<Groups> g;
// Get the data from the database using dapper
using( var con = DataAccessMaster.GetOpenConnection( ) ) {
using( var multi = con.QueryMultiple( sprocname, new { StartDate = fromDate, EndDate = toDate }, commandType:CommandType.StoredProcedure ) ) {
e = multi.Read<Events>( ).ToList( );
g = multi.Read<Groups>().ToList();
}
}
// Only put the groups that belong to one another within the related event so that when we goto bind it will be painless
foreach ( var ev in e ) {
ev.EventGroups = new List<Groups>();
foreach ( Groups group in g.Where( Groups => ( ev.EventID == Groups.EventID ) ) ) {
ev.EventGroups.Add( group );
}
}
return e;
我觉得最后一个块可以重写得更干净。
I feel like the last block could be rewritten more cleanly than it is. What can I do to make this cleaner?
推荐答案
您可以使用 Enumerable.ToList Extension Method 将IEnumerable< T>成为新的列表< T>:
You can use the Enumerable.ToList Extension Method to turn an IEnumerable<T> into a new List<T>:
foreach (var ev in e)
{
ev.EventGroups = g.Where(groups => ev.EventID == groups.EventID)
.ToList();
}
这篇关于我该如何改写这个更LINQy?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文