我有一个LINQ查询,如下所示:
var query = from r in Context.Role()
join rg in Context.RoleGroup() on r.RoleId equals rg.FkRoleId
where r.RoleName != "guests" && r.RoleName != "SystemAdmin"
select rg;
但是,我还需要一个附加条件,即不应根据外部条件排除SystemAdmin(例如,登录用户是超级用户)。
根据建议here,我修改了如下查询:
var query = from r in Context.Role()
join rg in Context.RoleGroup() on r.RoleId equals rg.FkRoleId
where r.RoleName != "guests" && (userId == 1 || r.RoleName != "SystemAdmin")
select rg;
但即使userId不是1,我仍然可以在输出中看到SystemAdmin角色。如何获得所需的输出?
答案 0 :(得分:0)
将此代码用于动态where条件:
if (printType == "BatchWise")
{
whereCondition = "BatchNo.Equals(@" + 0 + ")";
array[0] = Convert.ToString(printNumber);
}
else
{
whereCondition = "TagNo.Equals(@" + 0 + ")";
array[0] = Convert.ToString(printNumber);
}
using (var inventoryDb = new InventoryDb())
{
var pricingInfo = (from price in invDb.Pricing.AsNoTracking()
where tagDtl.AvailableQuantity > 0
select new
{
PricingNo = price.PricingNo,
............
}).Where(whereCondition, array).ToList();
}
答案 1 :(得分:0)
查询如下:
var query = from r in Context.Role()
join rg in Context.RoleGroup() on r.RoleId equals rg.FkRoleId
where r.RoleName != "guests" && (userId == 1 || (userId> 1 && r.RoleName != "SystemAdmin"))
select rg;