簡體   English   中英

C#-如果查詢中存在空值,則使用Linq獲取數據

[英]C# - Using Linq get data if null value exist in query

我有兩個數據表

var userList1 = from myRow in dt.AsEnumerable()
                where myRow.Field<bool?>("IsActive1") == null 
                      ? true 
                      : myRow.Field<bool?>("IsActive1") == true
                select myRow;

var userList2 = from myRow in dt1.AsEnumerable()
                select myRow;

dt1表顯示如下:

在此處輸入圖片說明

使用此Linq查詢,

var objUserSetUp1 = (from A in userList1
                     join B in userList2 
                     on new 
                     { 
                         UserId = A.Field<Int64?>("Id") == null 
                                      ? 0 
                                      : A.Field<Int64>("Id") 
                     } 
                     equals new 
                     { 
                         UserId = B.Field<Int64?>("UserId") == null 
                                      ? 0 
                                      : B.Field<Int64>("UserId") 
                     }
                     select new
                     {
                         UserId = A.Field<Int64>("Id"),
                         FirstName = A.Field<string>("FirstName"),
                         SurName = A.Field<string>("SurName"),
                         Computer_Name = A.Field<string>("Computer_Name"),
                         IP_Address = A.Field<string>("IP_Address"),
                         LogInTime = A.Field<string>("LogInTime") == null 
                                          ? "UnKnown" 
                                          : A.Field<string>("LogInTime"),
                         UserName = A.Field<string>("UserName"),
                         Password = A.Field<string>("Password"),
                         login_Id = A.Field<Int64?>("login_Id") == null 
                                        ? 0 : 
                                        A.Field<Int64?>("login_Id"),
                         docCount = B.Field<Int64>("docCount")
                     }).ToList();

如果UserId為null,我又如何也要獲取docCout字段值。 如何在查詢中執行此操作?

我認為您需要一個Left Outer Join ,其中外部聯接的默認值(即,當不存在匹配記錄時)是userList2條目,其中Field("UserId")null

參見下文(未經測試,但您知道了!):

    var objUserSetUp1 = (from A in userList1
                        join B in userList2 
                            on A.Field<Int64?>("Id") equals B.Field<Int64?>("UserId")
                            into BGroup
                        from C in BGroup.DefaultIfEmpty(userList2.Single(u => u.Field<Int64?>("UserId") == null))
                        select new
                        {
                            UserId = A.Field<Int64>("Id"),
                            FirstName = A.Field<string>("FirstName"),
                            SurName = A.Field<string>("SurName"),
                            Computer_Name = A.Field<string>("Computer_Name"),
                            IP_Address = A.Field<string>("IP_Address"),
                            LogInTime = A.Field<string>("LogInTime") == null
                                            ? "UnKnown"
                                            : A.Field<string>("LogInTime"),
                            UserName = A.Field<string>("UserName"),
                            Password = A.Field<string>("Password"),
                            login_Id = A.Field<Int64?>("login_Id") == null
                                        ? 0 :
                                        A.Field<Int64?>("login_Id"),
                            docCount = C.Field<Int64>("docCount")
                        }).ToList();

暫無
暫無

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

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