首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

linq to entity,返回自定义类型的List,该怎么处理

2012-05-13 
linq to entity,返回自定义类型的List刚接触linq 写了个语句,想知道怎么样才能直接把查询结果转成自定义的

linq to entity,返回自定义类型的List
刚接触linq 写了个语句,想知道怎么样才能直接把查询结果转成自定义的类型

C# code
    protected List<ProductT> GetProduct()    {        using (defaultModel.defaultEntities edata = new defaultModel.defaultEntities())        {            List<int> ids = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };            var pp = (from p in edata.Product                      join t in edata.Type on p.type_id equals t.type_id                      join ps in edata.Product_Sort on p.product_id equals ps.product_id                      where ids.Any(c => c == ps.sort_id)                       select new { t.type_name, p.product_id, p.product_order, p.product_name })                      .Distinct().OrderBy(p => p.product_order).Skip(0).Take(10);            return //这里怎么才能返回自定义的集合?           }    }    public ProductT product { get { return Page.GetDataItem() as ProductT; } }    public class ProductT {        public int product_id { get; set; }        public int product_name { get; set; }        public string type_name { get; set; }        public string product_order { get; set; }    }



直接使用ToList()会报错, 只能返回object, 是不是要用什么匿名类型,泛型之类的,这些概念都不太懂

[解决办法]
select new
改成 
select new ProductT
[解决办法]
List<int> ids = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var pp = (from p in edata.Product
join t in edata.Type on p.type_id equals t.type_id
join ps in edata.Product_Sort on p.product_id equals ps.product_id
where ids.Any(c => c == ps.sort_id) 
select new { type_name=t.type_name, product_id=p.product_id, product_order=p.product_order, product_name=p.product_name })
.Distinct().OrderBy(p => p.product_order).Skip(0).Take(10);
这样试试
[解决办法]
想要强类型,就必须 new ProductT 这个本身也不属于 DataContext 里的类型

热点排行