【发布时间】:2014-09-18 10:43:46
【问题描述】:
我有一个要求,我必须对两个表执行连接并使用 Linq to Sql 返回返回行的列表。
public List<T> GetProductList()
{
var popupList = (from p in this.Products
join c in this.Categories
on p.CategoryID equals c.CategoryID
select new
{
ProductID = p.ProductID,
ProductName= p. ProductName,
CategoryID = c.CategoryID,
CategoryName = c.CategoryName,
DateCreated = p.DateCreated,
IsActive = p.IsActive
}).OrderBy(p => p.CategoryName);
return popupList;
}
有没有办法在 C# 中做到这一点?
【问题讨论】:
-
用
dynamic替换T -
但是你为什么要匿名呢?为什么不直接创建一个 POCO?
-
并在末尾添加一个 ToList()。
标签: c# sql linq join anonymous-types