我在用户组中有动态字段,我想根据用户组用户的身份来选择它们。
基本上我想模拟查询,.Where(x => x.UserGroupId == x || ...
因为否则它会进行大约 20 个查询以获取动态字段。
也许我可以以某种方式传递整数数组,UserGroupId
它会模拟查询||
。
这是我的示例,两个结果输出相同,唯一的区别是第一个对数据库有 20 个查询,第二个只有 1 个。
public IEnumerable<UserGroup> UserGroups
{
get
{
var db = new MainDataContext();
return db.UserGroupUsers.Where(x => x.UserId == this.Id).Select(x => x.UserGroup);
}
}
public IEnumerable<UserDynamicField> DynamicFields
{
get
{
var db = new MainDataContext();
var fields = this.UserGroups.SelectMany(x => x.UserGroupDynamicFields); // 20+ queries
var fields2 = db.UserGroupDynamicFields.Where(x =>
x.UserGroupId == 1 ||
x.UserGroupId == 2 ||
x.UserGroupId == 3 ||
x.UserGroupId == 4 ||
x.UserGroupId == 5 ||
x.UserGroupId == 6 ||
x.UserGroupId == 7 ||
x.UserGroupId == 8 ||
x.UserGroupId == 9 ||
x.UserGroupId == 10); // 1 query, maybe I can somehow pass array of Id's here?
}
}