日期:2014-05-20  浏览次数:20992 次

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


    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)