gpt4 book ai didi

c# - 我如何重写它以使其更加 LINQy?

转载 作者:行者123 更新时间:2023-12-02 19:36:46 25 4
gpt4 key购买 nike

我这里有这组数据。事件有一个属性 EventGroups其类型为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;

我觉得最后一个 block 可以重写得比现在更干净。我该怎么做才能使它更干净?

最佳答案

您可以使用Enumerable.ToList Extension Method将 IEnumerable 转换为新的 List :

foreach (var ev in e)
{
ev.EventGroups = g.Where(groups => ev.EventID == groups.EventID)
.ToList();
}

关于c# - 我如何重写它以使其更加 LINQy?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6434601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com