1
它與存儲過程數據的所有啓動導致(平紋結構):平原數據源到嵌套列表C#
IdGrandad,GrandadName,IdDad,DadName,IdChild,ChildName
響應應該是嵌套實體列表。
我得到的數據轉換成SqlDataReader的 ......那麼,來這裏的循環:
var grandadList = new List<Grandad>();
while (reader.Read())
{
// Properties setters.
Grandad grandadItem = BindGrandad(reader);
Dad dadItem = BindDad(reader);
Child childItem = BindChild(reader);
// Entities finders.
Func<Grandad, bool> foundGrandad = (item => item.Identity.Equals(grandadItem.Identity));
Func<Dad, bool> foundDad = (item => item.Identity.Equals(dadItem.Identity));
if (!grandadList.Any(foundGrandad))
grandadList.Add(grandadItem);
// Here comes the horror-code...
if (!grandadList.Where(foundGrandad).SelectMany(item => item.DadList).ToList().Any(foundDad))
grandadList.SingleOrDefault(foundGrandad)?
.DadList.Add(dadItem);
grandadList.SingleOrDefault(foundGrandad)?
.DadList.SingleOrDefault(foundDad)?
.ChildList.Add(childItem);
}
的我如何簡化循環代碼的任何想法?
在此先感謝!