英孚如何只在模型中包含一些子结果? [英] EF. How to include only some sub results in a model?
本文介绍了英孚如何只在模型中包含一些子结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图选择用户列表,并为每个用户JobTitle选择正确的语言,具体取决于用户选择的strLang。
I'm trying to select list of Users and for each User JobTitle in correct language depended of strLang selected by user.
类似的东西:
IList<User> myData;
myData = Context.Users.Where(u => u.Location == strLocation)
.Include(u => u.JobTitles.Where(e => e.Language == strLang))
.ToList();
但是似乎Include不喜欢Where子句
But it seems Include doesn't like Where clause
推荐答案
您不能有条件地只包含相关集合的几个实体,因此您应该使用投影来获取所需的东西:
You can't conditionally include only a few entities of a related collection, so you should use projection to get the stuff you need:
IList<User> myData;
var temp = Context.Users.Where(u => u.Location == strLocation)
.Select(u => new
{
User = u;
Locations = u.JobTitles.Where(e => e.Language == strLang));
});
foreach(var t in temp)
{
User user = t.User;
user.Locations = t.Locations;
myData.Add(user);
}
这篇关于英孚如何只在模型中包含一些子结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文