LINQ query on entities with join returning items by category

Viewed 543

I'm using Entity Framework core. I have the following entities:

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; }
}

I'd like to get a grouping whereby my key is my Category and I have a list of Products. I'd like to do this running a single query against the DB.

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

This runs the query I want and seems to produce a list with an anonymous object that has a Category and Product. If I then do the following, it kind of does what I want:

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

The Key is fine, but the list of values seems to repeat the Categories. Is there a way to make it so I have a Key that's the Category and then the values is a list of Product objects? Would prefer method syntax, but at this point I can hopefully figure out query syntax if that's what somebody can get me.

4 Answers

Try following :

   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; }
    }

This will great a list of products grouped 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; }
}

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);
    }
}

The correct solution would be something like this:

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());

This is how I ended up solving it:

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