foreach (var type in types)
{
BaseType existing = myOrderdList.Find(x => x.Id == type.Id);
if (existing == null)
{
myOrderdList.Add(type);
}
else
{
type.BaseProducts.Add(existing.BaseProducts[0]);
myOrderdList.Add(type);
}
}
我有一个BaseType
课程列表。每个BaseType
都有一个BaseProducts
IList列表。
以下是我的意见:
public class BaseProduct
{
public int ID { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public string Path { get; set; }
}
这是我的BaseType类:
public class BaseType
{
public int Id { get; set; }
public string CatalogName { get; set; }
public List<BaseProduct> BaseProducts { get; set; }
}
我有一个Stored Procudre会返回这样的记录:
Id Name ProductName Price
7 Digital Cameras 35 Sony DSCWX350 Digital Camera Black 600
7 Digital Cameras 36 Sony DSCH400 Digital Camera Black 600
8 Mobiles 15 Samsung Galaxy S4 VE GTI9515 Black 4G LTE 600
8 Mobiles 16 Samsung Galaxy SIII Neo Dual Sim 600
8 Mobiles 17 Samsung Galaxy Note 3 Neo Duos Smarthphone 600
现在我的服务方法将为每一行创建一个basetype对象。
因此它将返回一个basetype列表。
如何按名称对结果进行分组不使用IList.GroupBy方法?
到目前为止我得到了这个:
var types = ReadFromTheServiceMethd();
List<BaseType> myOrderdList = new List<BaseType>();
foreach (var type in types)
{
BaseType existing = myOrderdList.Find(x => x.Id == type.Id);
if (existing == null)
{
myOrderdList.Add(type);
}
else
{
type.BaseProducts.Add(existing.BaseProducts[0]);
myOrderdList.Add(type);
}
}
答案 0 :(得分:1)
实现此目标的一种方法是拥有Dictionary<int, BaseType>
,其中int
是Id
的{{1}}。考虑到这一点,如果您在执行存储过程时也能够为BaseType
字段返回Id
,则可以执行以下操作:
Name