【发布时间】:2013-07-17 21:45:27
【问题描述】:
我在用户组中有动态字段,我想根据用户组的用户来选择它们。
基本上我想模拟像.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?
}
}
【问题讨论】:
标签: c# linq entity-framework entity-framework-5