簡體   English   中英

LINQ與group by用於嵌套列表

[英]LINQ with group by for nested list

如果我的主要對象使用帶有嵌套列表的Linq,則嘗試過濾並獲取列表。 我需要的是List<Category>

class Category contains List<Order> and List<Price>

在此處輸入圖片說明

但是我得到的結果被兩個子對象分組,因此我沒有得到確切的結果。 我想單獨分組以得到結果。 以下是我的Linq查詢,

我在這里做錯了什么?

var resultList = (
    from category in Connection.Table<Category>().ToList()
    join order in Connection.Table<Order>().ToList()
        on category.Id equals order.CategoryId
    join price in Connection.Table<Price>().ToList()
        on category.Id equals price.CategoryId
    where category.Id == TestID
    select new {
        category.ID,
        category.Name,
        order,
        price
    } into grou
    group grou by new {
        grou.Id
    } into grp
    select new Category {
        Id = grp.Id,
        Order = grp.Select(x => x.order).ToList(),
        Price = grp.Select(x => x.price).ToList()
    }).ToList();

嘗試以下類似的方法。 至少有一種情況您使用ID而不是ID:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

         }
    }
    public class cConnection
    {
        public cConnection Connection { get; set; }

        public List<cTable<Entity>> Table<Entiry>() { return null; } 

        public void Test()
        {
            int TestID = 123;

            var resultList = (from mainObjectEntity in Connection.Table<MainObjectEntity>().ToList()
                    join child1Entity in Connection.Table<Child1Entity>().ToList()
                    on mainObjectEntity.Id equals child1Entity.MainObjectEntityId
                    join child2Entity in Connection.Table<Child2Entity>().ToList()
                    on mainObjectEntity.Id equals child2Entity.MainObjectEntityId
                    where mainObjectEntity.TestId == TestID
                    select new
                    {
                        id = mainObjectEntity.TestId,
                        mainObjectEntity = mainObjectEntity,
                        child1 = child1Entity,
                        child2 = child2Entity
                    })
                    .GroupBy(x => x.id)
                    .Select(grp => new {
                        TestID = grp.FirstOrDefault().id,
                        Child1Entity = grp.Select(x => x.child1).ToList(),
                        Child2Entity = grp.Select(x => x.child2).ToList()
                     }).ToList();
        }
    }
    public class cTable<T> : Entity
    {
        public int MainObjectEntityId { get; set; }
        public int TestId { get; set; }
    }
    public class Entity
    {
        public int Id { get; set; }
    }
    public class Child1Entity : Entity
    {

    }
    public class Child2Entity : Entity
    {
    }
    public class MainObjectEntity : Entity
    {
    }


}

暫無
暫無

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

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