如何在foreach linq两级查询中读取
我在查询和创建两级关系时遇到的问题如下: 拉姆达:如何在foreach linq两级查询中读取,linq,ienumerable,Linq,Ienumerable,我在查询和创建两级关系时遇到的问题如下: 拉姆达: public IList GetMasterDetailsFilterLang(string language) { var query = (_ourServiceCategories .Where(c => (c.Language == "fa-IR")) .Select(
public IList GetMasterDetailsFilterLang(string language)
{
var query = (_ourServiceCategories
.Where(c => (c.Language == "fa-IR"))
.Select(
c =>
new
{
CatId = c.Id,
CatName = c.Title,
OurServices = c.OurServices
.Select(
o =>
new
{
ServId = o.Id,
ServName = o.Title
}
)
}
)).ToList();
return query;
}
林克:
from c in OurServiceCategories
where c.Language == "fa-IR"
select new
{
CatId = c.Id,
CatName = c.Title,
OurServices = from o in c.CategoryOurServices
select new
{
ServId = o.Id,
ServName = o.Title
}
}
结果:
现在:我不知道如何使用这个,Foreach如何阅读
我想这样读:
var ds = OurServiceService.GetMasterDetailsFilterLang(_LangSar);
foreach (var d in ds)
{
//Read Master example : d.Id,d.Title
//do something
foreach (var details in d)
{
//Read Details example : details.Id,details.Name
//do something
}
}
对于d.OurService中的每个var细节您的意思是这样的吗
var ds = from c in OurServiceCategories
where c.Language == "fa-IR"
select new
{
CatId = c.Id,
CatName = c.Title,
OurServices = from o in c.CategoryOurServices
select new
{
ServId = o.Id,
ServName = o.Title
}
};
foreach (var d in ds)
{
//here you can access d.CatId & d.CatName
foreach (var details in d.OurServices)
{
//here you can access details.ServId & details.ServName
}
}
问题是什么?您是否希望获得与屏幕截图相同的结果,但使用2级foreach而不是LINQ,就像您已经做的那样?请使用完整代码编辑文章
var ds = from c in OurServiceCategories
where c.Language == "fa-IR"
select new
{
CatId = c.Id,
CatName = c.Title,
OurServices = from o in c.CategoryOurServices
select new
{
ServId = o.Id,
ServName = o.Title
}
};
foreach (var d in ds)
{
//here you can access d.CatId & d.CatName
foreach (var details in d.OurServices)
{
//here you can access details.ServId & details.ServName
}
}