2010-01-23 58 views
1

我使用Linq To Sql在DAL層中連接查詢,我如何自動爲LinqToSql連接方法定義返回List<T>如何爲LinqToSql加入定義返回列表<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層?

回答

2

您仍然必須爲返回類型創建一個自定義類,因爲您無法從方法返回匿名類。你可以避免聲明,雖然使用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(); 
} 
+0

好了,你可以* *匿名類型的返回列表...你就不能在一個強類型的方式這樣做。 – 2010-01-23 16:59:45

+0

然後如何將匿名類型的列表從DAL層傳遞到BLL層? – Mike108 2010-01-23 17:38:45

相關問題