简体   繁体   English

如何设置条件C#if语句和LINQ where语句?

[英]How can I set up a conditional C# if statement and LINQ where statement?

The details of my situation: If a user has permissions to view a certain location's items, I need the query to select items that have a facility that matches the permissions the user has. 我的情况的详细信息:如果用户有权查看某个位置的项目,则我需要查询以选择具有与用户所拥有的权限相匹配的工具的项目。 A user might have permissions to multiple facilities. 用户可能具有对多个设施的权限。 There may be a user that has access to LOC1, LOC2 and also LOC3. 可能有一个用户有权访问LOC1,LOC2以及LOC3。 There may be a user with only access to LOC1. 可能有一个用户只能访问LOC1。 I might be overlooking something extremely simple to solve this. 我可能会忽略一些极其简单的解决方案。

if(System.Web.HttpContext.Current.User.IsInRole("App_Inventory_LOC1_Access")) 
{
    items = items.Where(s => s.Facility == "LOC1");
}

if(System.Web.HttpContext.Current.User.IsInRole("App_Inventory_LOC2_Access")) 
{
    items = items.Where(s => s.Facility == "LOC2");
}

if(System.Web.HttpContext.Current.User.IsInRole("App_Inventory_LOC3_Access")) 
{
    items = items.Where(s => s.Facility == "LOC3");
}

So you just construct a list of allowed facilities, and then check if s.Facility is in those: 因此,您只需构造一个允许的设施列表,然后检查s.Facility是否在其中:

var facilities = new List<string>();
if(System.Web.HttpContext.Current.User.IsInRole("App_Inventory_LOC1_Access")) 
{
    facilities.Add("LOC1");
}
// same for other facilities
// ...

items = items.Where(s => facilities.Contains(s.Facility));

To further simplify it, you could group roles and facilities in some kind of map, and iterate through that - will make it much easier to add new facilities for example. 为了进一步简化,您可以在某种地图中对角色和设施进行分组,然后进行迭代-例如,这将使添加新设施变得更加容易。

You can refactor that with a dictionary to map user role to facility: 您可以使用字典将其重构以将用户角色映射到设施:

var userFacilityMapping = new Dictionary<string, string>
{
    ["App_Inventory_LOC1_Access"] = "LOC1",
    ["App_Inventory_LOC2_Access"] = "LOC2",
    ["App_Inventory_LOC3_Access"] = "LOC3",
};
var userFacilities = userFacilityMapping
    .Where(x => HttpContext.Current.User.IsInRole(x.Key))
    .Select(x => x.Value)
    .ToArray();

items = items.Where(x => userFacilities.Contains(x.Facility));

I think you're overwriting your result set. 我认为您正在覆盖结果集。 Say if you have access to LOC1 and LOC3, it will first choose all the LOC1 items, and then from the LOC1 items it will choose the LOC3 items which is empty. 假设您有权访问LOC1和LOC3,它将首先选择所有LOC1项目,然后从LOC1项目中选择将为空的LOC3项目。

You want to only filter the original item set. 您只想过滤原始项目集。 Something like 就像是

var results = new List<Item>();

if(System.Web.HttpContext.Current.User.IsInRole("App_Inventory_LOC1_Access")) 
{
    results.AddRange(items.Where(s => s.Facility == "LOC1"));
}

if(System.Web.HttpContext.Current.User.IsInRole("App_Inventory_LOC2_Access")) 
{
    results.AddRange(items.Where(s => s.Facility == "LOC2"));
}

if(System.Web.HttpContext.Current.User.IsInRole("App_Inventory_LOC3_Access")) 
{
    results.AddRange(items.Where(s => s.Facility == "LOC3"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM