简体   繁体   English

如何在连接两个表时将Linq中的匿名对象列表返回给Sql

[英]How to return a list of anonymous object in Linq to Sql on joining two tables

I have one requirement in which I have to perform a join on two tables and return a list of the returned rows using Linq to Sql. 我有一个要求,我必须在两个表上执行连接,并使用Linq返回返回行的列表到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;
}

Is there any way to do this in C#? 有没有办法在C#中做到这一点?

You could declare a class, like below -: 您可以声明一个类,如下所示:

public class ProductCategoryView
{
    public int ProductID        { get; set; }
    public strig ProductName    { get; set; }
    public int CategoryID       { get; set; }
    public string CategoryName  { get; set; }
    public DateTime DateCreated { get; set; }
    public bool IsActive        { get; set; }
}

which will have as properties the values you want to have each row of your join. 它将具有您希望连接的每一行的值作为属性。

note I may be wrong of the types I choosed for some of your properties. 请注意,我为某些属性选择的类型可能有误。 So you have to correct them correspondingly. 所以你必须相应地纠正它们。

Then you have to refactor your query: 然后你必须重构你的查询:

public List<ProductCategoryView> GetProductList()
{
    var popupList = (from p in this.Products
                     join c in this.Categories 
                     on p.CategoryID equals c.CategoryID
                     select new JoinView
                     {
                         ProductID = p.ProductID,
                         ProductName= p. ProductName,
                         CategoryID = c.CategoryID,
                         CategoryName = c.CategoryName,
                         DateCreated = p.DateCreated,
                         IsActive = p.IsActive
                     }).OrderBy(p => p.CategoryName)
                       .ToList();
    return popupList;
}

I have also found a way of doing this using IQueryable return type. 我还找到了一种使用IQueryable返回类型的方法。 Could you suggest me the advantages and disadvantages of using IQueryable as return type for anonymous types. 你能否告诉我使用IQueryable作为匿名类型的返回类型的优点和缺点。

public IQueryable 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
                     };
    return popupList.OrderBy(p => p.CategoryName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM