簡體   English   中英

C#中的Linq查詢中的枚舉條件

[英]Enum Condition in Linq Query in C#

我想在Linq查詢中添加Enum條件,即使枚舉數據成員為null或為空

我已經在模型中為過濾器目的添加了ALL ,以便如果用戶選擇全部,那么應該顯示所有數據

網頁畫面

資料模型:

    public partial class AuditTable
    {
        public int ID { get; set; }
        public int CompanyId { get; set; }
        public int KeyFieldID { get; set; }
        public System.DateTime DateTimeStamp { get; set; }
        public EntityType DataModel { get; set; }
        public string ValueBefore { get; set; }
        public string ValueAfter { get; set; }
        public string Changes { get; set; }
        public AuditActionType AuditActionTypeENUM { get; set; }

        public int EmployeeId { get; set; }
        public string EmployeeCode { get; set; }
        public string Remarks { get; set; }
        public string IPAddress { get; set; }
        public string UserName { get; set; }
    }
 public enum AuditActionType
    {
        All = 1,
        Create,
        Update,
        Delete
    }

    public enum EntityType
    {
        All = 1,
        BasicDetails,
        EmployeeDetails,
        PersonalDetails                
    }

下面的代碼工作正常,但必須重復相同的查詢4次。 我想將以下查詢合並為一個

if (eType == EntityType.All)
 if (aType == AuditActionType.All)
  AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList(); 
 else
  AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.AuditActionTypeENUM == aType && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList(); 
else
if (aType == AuditActionType.All)
 AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.DataModel == eType && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList();
else
 AuditTrail = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.AuditActionTypeENUM == aType && s.DataModel == eType && s.EmployeeCode.Contains(code) && s.UserName.Contains(username)).OrderByDescending(s => s.DateTimeStamp).ToList(); 

您可以鏈接Where()語句。

var baseQuery = ent.tblAuditTable.Where(s => s.KeyFieldID == ID && s.EmployeeCode.Contains(code) && s.UserName.Contains(username));

var realQuery = baseQuery;

if (eType != EntityType.All){
  realQuery = realQuery.Where(s=>s.DataModel == eType);
}

if (aType != EntityType.All){
  realQuery = realQuery.Where(x=>s.AuditActionTypeENUM == aType);
}

var result = realQuery.OrderByDescending(s => s.DateTimeStamp).ToList()

AuditTrail = result;

全部集中在一個查詢中?

像這樣:

bool eTypeIsAll = eType == EntityType.All;
bool aTypeIsAll = aType == AuditActionType.All;
AuditTrail =
    ent.tblAuditTable
       .Where(s => s.KeyFieldID == ID
                   && (
                       eTypeIsAll ? (
                            (
                                !aTypeIsAll ?
                                    s.AuditActionTypeENUM == aType
                                    : true
                            )

                       )
                       : (
                            (
                                !aTypeIsAll ?
                                    s.AuditActionTypeENUM == aType
                                    : true
                            )
                            && s.DataModel == eType

                       )

                   )
                   && s.EmployeeCode.Contains(code)
                   && s.UserName.Contains(username))
        .OrderByDescending(s => s.DateTimeStamp)
        .ToList();

就個人而言,我更喜歡這種方式。 但是很多人說@Jehof方法更具可讀性。

您應該使用IQueryables。 無需單行編寫查詢。 您可以將其傳播到多個子查詢中。

var queryAble= ent.tblAuditTbale.where(s=>s.KeyFieldId ==Id);

if(eType !=EntityType.All)  
 queryAble = queryAble.where(s=> s.DataModel==eTYpe);

If(aType!= AuditActionType.All)    
 queryAble = queryAble.where(s=> s.AuditActionTypeENUM == aType);

最后(查詢將立即執行)

AuditTrail = queryAble.ToList();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM