2016-09-23 15 views
-2

LINQグループについて質問があります。LINQでデータをグループ化できません

私は、結果セットでGroupBy関数を使用し、それをグループ化することを指定する簡単な問題であると考えました。しかし、私のアイテムはグループ化されていないように見え、その代わりにGroupByの機能がなかったかのように表示されます。私はitemPkでグループ化したいですが、私はそれを行うように見えません。私はcategory.ItemFkItem.Itempkの両方でグループ化を試みましたが、運はありません。誰かが私にこの点についてのポインタを与えることができますか?

 var itemIds = items.Select(i => i.ItemId).ToList(); 
     var itemAndCatJoin = 

     from item in Context.SCS_Items 
     join category in Context.SCS_ItemCategories 
     on item.ItemPk equals category.ItemFk 
     into temp 
     from category in temp.DefaultIfEmpty() 
     select new ExportItemTable 
     { 
     Category = category, 
     Item = item 
     }; 

     return itemAndCatJoin.Where(i => itemIds.Contains(i.Item.ItemPk)) 
         .GroupBy(n => new {n.Item, n.Category}) 
         .Select(i => new ExportableItem 
         { 

          ItemPk = i.Key.Item.ItemPk, 
          Name = i.Key.Item.Name, 
          Description = i.Key.Item.Description, 
          Price = i.Key.Item.Price, 
          Category = i.Key.Category.Category.Category_Name, 
          GLDepartment = i.Key.Category.GL_Department.Name ?? "", 
          GLName = i.Key.Category.GL_Name.Name ?? "", 
          StartDate = i.Key.Item.StartDate, 
          EndDate = i.Key.Item.EndDate, 
          FiscalYear = i.Key.Item.SCS_FiscalYear.Name, 
          School = i.Key.Item.School != null ? i.Key.Item.School.School_Name : i.Key.Item.Board.Board_Name, 
          Beneficiary = i.Key.Item.SCS_Beneficiary.Name, 
          Quantity = i.Key.Item.MaxQuantity, 
          Deleted = i.Key.Item.DeletedFlag, 
          OptionalStudents = i.Key.Item.SCS_Attachments.Where(a => !a.IsRequired).SelectMany(a => a.SCS_StudentAttachments).Where(s => !s.DeletedFlag).Select(s => s.StudentFk).Distinct().Count(), 
          RequiredStudents = i.Key.Item.SCS_Attachments.Where(a => a.IsRequired).SelectMany(a => a.SCS_StudentAttachments).Where(s => !s.DeletedFlag).Select(s => s.StudentFk).Distinct().Count(), 
          IsPublic = i.Key.Item.IsPublic, 
          AllowRecurring = i.Key.Item.AllowRecurringPayments, 
          EffectiveCutoff = i.Key.Item.SCS_Attachments.Where(a => !a.DeletedFlag && a.CourseDropCutoff.HasValue).Select(a => a.CourseDropCutoff).OrderBy(a => a).FirstOrDefault(), 
          CreatedDate = i.Key.Item.CreatedDate 
         }).OrderBy(i => i.ItemPk).ToList(); 
    } 
+0

あなたはitempkでグループ化したいとします。これは、group by句では言及されていません。私はあなたがこれがうまくいくと信じている方法について混乱しています。したがって、あなたがどこに間違って行ったのかあなたに伝えるのは難しいです。グループ化する特定のキーがある場合、そのキーはグループ句のどこかになければなりません。 –

答えて

0

あなたgroupbyyは確かにあなたのために何もしていない、あなたはここで

.GroupBy(n => n.Category) 
+0

私はそれを理解していますが、カテゴリとアイテムによってグループ化されているものは何ですか? – Seamy

+0

あなたは何を期待しているのか分かりませんか?あなたはアイテムとカテゴリのみを持っていますか?両方のグループ分けは無意味ですか? –

-3

のように....

によってどのグループにGROUPBYを伝える必要があり、あなたのグループに簡単な例であります質問:

class Program 
{ 
    static void Main() 
    { 
     var allItems = GetAllItems(); 
     var groups = from item in allItems 
      group item by item.Category 
      into newGroup 
      select newGroup; 

     foreach (var group in groups) 
     { 
      Console.WriteLine($"\nCategory: {group.Key}"); 
      foreach (var item in group) 
      { 
       Console.WriteLine($"{item.Name}: {item.Price}"); 
      } 
     } 
     Console.ReadLine(); 
    } 

    static List<Category> GetAllCategories() 
    { 
     return new List<Category>() 
     { 
      new Category() { Id = 1, Name = "Programming Books" }, 
      new Category() { Id = 2, Name = "Fiction Books" } 
     }; 
    } 

    static List<Item> GetAllItems() 
    { 
     return new List<Item>() 
      { 
       new Item() { Id = 1, Name = "Embedded Linux", Category = 1, Price = 9.9 }, 
       new Item() { Id = 2, Name = "LINQ In Action", Category = 1, Price = 36.19 }, 
       new Item() { Id = 3, Name = "C# 6.0 and the .NET 4.6 Framework", Category = 1, Price = 40.99 }, 
       new Item() { Id = 4, Name = "Thinking in LINQ", Category = 1, Price = 36.99 }, 
       new Item() { Id = 5, Name = "The Book Thief", Category = 2, Price = 7.99 }, 
       new Item() { Id = 6, Name = "All the Light We Cannot See", Category = 2, Price = 16.99 }, 
       new Item() { Id = 7, Name = "The Life We Bury", Category = 2, Price = 8.96 } 
      }; 
    } 
} 

public class Item 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public double Price { get; set; } 
    public int Category { get; set; } 
} 

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

この例は、LINQを初めて使用する人にとっては簡単です。私はあなたの特定の問題のためにそれを動作させるためにいくつかの調整を行うことができると確信しています。これが役立つことを願っています。

+0

とグループ化はどこですか? – pinkfloydx33

関連する問題