[英]Nested Linq in c#
我想使用 linq 作為樹結構提取一個列表。
{
"details": [
{
"description":"This is description 1",
"Name": "Name 1"
},
{
"description":"This is description 2",
"Name": "Name 2"
}
],
"price": 100
}
我手頭有一個 detailsDto 作為 List<>,我將在 Details 字段中使用它
來自“m”實例的屬性將在 detailsDto 中綁定。 那就是我面臨如何去做的問題。 “m”實例中可用的描述和名稱字段
var data = await Task.FromResult((
from c in _context.C
join mc in _context.MC on c.VId equals mc.VId
join m in _context.M on mc.Id equals m.mcId
where mc.Id == mcId && c.Id == CId
select new MainDto()
{
Price = mc.Price,
// Details =
}
).FirstOrDefault()
);
可能你需要這個查詢:
var data = await (
from c in _context.C
join mc in _context.MC on c.VId equals mc.VId
where mc.Id == mcId && c.Id == CId
select new MainDto()
{
Price = mc.Price,
Details = _context.M.Select(m => new DetailsDto
{
Name = m.Name,
description = m.Description,
}).ToList()
}
).FirstOrDefaultAsync();
這應該有幫助
{
Price = mc.Price,
Details = m.Select(x => new DetailDto
{
Description = x.Description,
Name = x.Name
}).ToList()
}
它將為m
列表中的每個元素創建一個DetailDto
class 的新實例,並將Description
和Name
屬性的值分配給DetailDto
實例中的相應屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.