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;}
}
我想做的有两件事:
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
}
}