如何使用Linq
将集合映射到多个结果集中的对象我正在尝试将集合映射到他们的对象,但我得到了错误的结果
我的业务对象看起来像这样
public class ProductForAjax
{
public ProductForAjax()
{
this.DynamicProperties = new List<DynamicProperty>();
this.Categories = new List<Category>();
}
public int ProductId { get; set; }
public string ProductName { get; set; }
public decimal ProductPrice { get; set; }
public int ProductQuantity { get; set; }
public IEnumerable<DynamicProperty> DynamicProperties { get; set; }
public IEnumerable<Category> Categories { get; set; }
}
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
public class DynamicProperty
{
public int PropertyId { get; set; }
public string PropertyName { get; set; }
public string PropertyValue { get; set; }
}
这是我尝试将集合映射到对象的方式
var See = db.Database.SqlQuery<GetProduct>("EXEC sp_GetProduct @ProductId", par).ToList();
var grouped = (from c in See
group new
{
c.PropertyId,
c.PropertyName,
c.PropertyValue,
c.SubCategoryId,
c.SubCategoryName
}
by new
{
c.ProductId,
c.ProductName,
c.ProductPrice,
c.ProductQuantity
}).ToList();
ProductForAjax J = grouped.Select(c => new ProductForAjax()
{
ProductId = c.Key.ProductId,
ProductName = c.Key.ProductName,
ProductPrice = c.Key.ProductPrice,
ProductQuantity = c.Key.ProductQuantity,
DynamicProperties = c.Select(x => new DynamicProperty()
{
PropertyId = x.PropertyId,
PropertyName = x.PropertyName,
PropertyValue = x.PropertyValue
}
).ToList(),
Categories = c.Select(y => new Category()
{
CategoryId = y.SubCategoryId,
CategoryName = y.SubCategoryName
}).ToList()
}).FirstOrDefault();
我实际上是Linq的新手,所以请不要根据我的代码解决我的问题,因为我不知道它是否正确。那么请告诉我这个案例在现实世界中是如何解决的