How to use LINQ Distinct() with multiple fields

前端 未结 9 1357
借酒劲吻你
借酒劲吻你 2020-11-30 01:32

I have the following EF class derived from a database (simplified)

class Product
{ 
     public string ProductId;
     public string Product         


        
9条回答
  •  春和景丽
    2020-11-30 02:22

    public List GetBrandListByCat(int id)
        {
    
            var OBJ = (from a in db.Items
                       join b in db.Brands on a.BrandId equals b.Id into abc1
                       where (a.ItemCategoryId == id)
                       from b in abc1.DefaultIfEmpty()
                       select new
                       {
                           ItemCategoryId = a.ItemCategoryId,
                           Brand_Name = b.Name,
                           Brand_Id = b.Id,
                           Brand_Pic = b.Pic,
    
                       }).Distinct();
    
    
            List ob = new List();
            foreach (var item in OBJ)
            {
                ItemCustom2 abc = new ItemCustom2();
                abc.CategoryId = item.ItemCategoryId;
                abc.BrandId = item.Brand_Id;
                abc.BrandName = item.Brand_Name;
                abc.BrandPic = item.Brand_Pic;
                ob.Add(abc);
            }
            return ob;
    
        }
    

提交回复
热议问题