Linq 如何选择只有特定列的父行和子行

Linq 如何选择只有特定列的父行和子行,linq,linq-to-entities,entity-framework-4.1,Linq,Linq To Entities,Entity Framework 4.1,因此,我有两个表的布局如下(但未命名): 及 我构建了一个简单的类 public class SimpleCollection { public Guid ID { get; set; } public string Title { get; set; } public IEnumerable<SimpleCollection> Elements { get; set; } } 公共类SimpleCollection { 公共Guid ID{get;set;

因此,我有两个表的布局如下(但未命名):

我构建了一个简单的类

public class SimpleCollection
{
    public Guid ID { get; set; }
    public string Title { get; set; }
    public IEnumerable<SimpleCollection> Elements { get; set; }
}
公共类SimpleCollection
{
公共Guid ID{get;set;}
公共字符串标题{get;set;}
公共IEnumerable元素{get;set;}
}
我想做的有两件事:

  • 在一次调用中返回所有父/子行
  • 只选择我需要的列,这样我就不需要额外的60/30列了
  • 我尝试了以下方法:

    var query = from A in dbContext.TableA
                from B in A.TableB
                select new SimpleCollection()
                {
                    ID = A.AID,
                    Title = A.Title,
                    Elements = select new SimpleCollection<string>()
                    {
                        ID = B.BID,
                        Title = B.Title
                    }
                };
    
    var query=来自dbContext.TableA中的
    从A表B中的B开始
    选择新建SimpleCollection()
    {
    ID=A.AID,
    头衔,头衔,
    元素=选择新的SimpleCollection()
    {
    ID=B.投标,
    头衔
    }
    };
    

    但它不喜欢将元素设置为select语句。

    我相信我已经考虑过这个问题了

    var query = from A in dbContext.TableA
                from B in A.TableB
                group B by A into g
                select new SimpleCollection()
                {
                    ID = g.Key.AID,
                    Title = g.Key.Title,
                    Elements = g.Select(x => new SimpleCollection {ID = x.BID, x.Title}).ToList()
                };
    
    var query = dbContext.TableA
                         .Select(p => new SimpleCollection()
                         {
                           ID = p.AID,
                           Title = p.Title,
                           Elements = p.TableBs
                                       .Select(c => new SimpleCollection()
                                       {
                                         ID = c.BID,
                                         Title = C.Title
                                       }
                         }                  
    
    g.ToList()
    不会将
    g
    的类型转换为
    SimpleCollection
    类型,是吗?不会
    query.ToList()
    返回重复的表格行吗?
    var query = from A in dbContext.TableA
                from B in A.TableB
                group B by A into g
                select new SimpleCollection()
                {
                    ID = g.Key.AID,
                    Title = g.Key.Title,
                    Elements = g.Select(x => new SimpleCollection {ID = x.BID, x.Title}).ToList()
                };
    
    var query = dbContext.TableA
                         .Select(p => new SimpleCollection()
                         {
                           ID = p.AID,
                           Title = p.Title,
                           Elements = p.TableBs
                                       .Select(c => new SimpleCollection()
                                       {
                                         ID = c.BID,
                                         Title = C.Title
                                       }
                         }