簡體   English   中英

LINQ 按類別查詢具有連接返回項的實體

[英]LINQ query on entities with join returning items by category

我正在使用實體框架核心。 我有以下實體:

public class Category {
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Product {
    public long Id { get; set; }
    public long CategoryId { get; set; }
    public string Name { get; set; }
}

我想要一個分組,其中我的鍵是我的類別,並且我有一個產品列表。 我想對數據庫運行單個查詢來執行此操作。

var data = context.Categories
            .Join(
                Products,
                c => c.Id,
                p => p.CategoryId,
                (c, p) => new {
                   Category = c,
                   Product = p
               }
            )
            .ToList();

這會運行我想要的查詢,並且似乎會生成一個包含匿名 object 的列表,該列表具有類別和產品。 如果我然后執行以下操作,它會做我想要的:

var grouped = data.GroupBy(x => new { x.Category });

鍵很好,但值列表似乎重復了類別。 有沒有辦法做到這一點,所以我有一個 Key 是 Category 然后值是 Product 對象的列表? 更喜歡方法語法,但在這一點上,如果有人能得到我,我希望能弄清楚查詢語法。

嘗試以下操作:

   class Program
    {
        static void Main(string[] args)
        {
            Context context = new Context();

            var results = (from c in context.Categories
                           join p in context.Products on c.Id equals p.CategoryId
                           select new { category = c, product = p })
                          .GroupBy(x => x.category.Id)
                          .ToList();
        }
    }
    public class Context
    {
        public List<Category> Categories { get; set; }
        public List<Product> Products { get; set; }
    }
    public class Category
    {
        public long Id { get; set; }
        public string Name { get; set; }
    }

    public class Product
    {
        public long Id { get; set; }
        public long CategoryId { get; set; }
        public string Name { get; set; }
    }

這將是一個按類別分組的產品列表

public class Category {
    public long Id { get; set; }
    public string Name { get; set; }
}

public class Product {
    public long Id { get; set; }
    public long CategoryId { get; set; }
    public string Name { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        ICollection<Category> categories = new Collection<Category>(new List<Category>());
        ICollection<Product> products = new Collection<Product>(new List<Product>());

        var data = categories
        .Join(
            products,
            c => c.Id,
            p => p.CategoryId,
            (c, p) => new {
               Category = c,
               Product = p
           }
        )
        .ToList();

        var grouped = data.GroupBy(o => o.Category, o => o.Product);
    }
}

正確的解決方案是這樣的:

var result = (from p in context.Products
              join c in context.Categories
                  on p.CategoryId equals c.Id
              select new
              {
                  Product = p,
                  Category = c
              })
              .ToList() // because still grouping object is not supported in EF Core we have to materialize first and then do our grouping
              .GroupBy(g => g.Category, g => g.Product)
              .ToDictionary(p => p.Key, p => p.ToList());

這就是我最終解決它的方式:

var grouped = data
    .GroupBy(x => new { x.Category })
    .Select(x => new {
        Category = x.Key.Category,
        Products = x.ToList().Select(x => x.Product).ToList()
    });;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM