我在DAL层使用Linq To Sql Join查询,如何为LinqToSql Join方法自动定义一个返回List<T>?
现在,我必须为每个LinqToSql Join方法的返回值手动定义一个List<CustomViewType>。
是否可以定义如下代码所示List<T>:
public static List<T> GetJoinList()
{
List<T> list = new List<T>();
list = from c in customers
join o in orders on o.customerid equals c.customerid
select new { CustomerID = c.CustomerId, OrderDate = o.OrderDate } ;
return list.ToList() ;
}实际上,我的意思是如何将匿名类型列表从DAL层传递到BLL层?
发布于 2010-01-24 00:09:07
您仍然必须为返回类型创建自定义类,因为您不能从方法返回匿名类。通过使用ToList()扩展方法,您可以避免声明和分配列表:
public static List<CustomViewType> GetJoinList()
{
return (from c in customers
join o in orders on o.customerid equals c.customerid
select new CustomViewType { CustomerID = c.CustomerId, OrderDate = o.OrderDate}).ToList();
}https://stackoverflow.com/questions/2123737
复制相似问题