2017-10-06 171 views
0

我有一個LINQ查詢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; 

但我仍然可以在輸出中看到SystemAdmin作用,即使用戶id不是1我怎樣才能得到需要的結果?

+2

如果使用標識不爲1,那麼你必須'r.RoleName = 「客人」 && (false || r.RoleName!=「SystemAdmin」)'這與你的第一個條件相同。這意味着用戶ID等於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; 
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(); 
      }