簡體   English   中英

LINQ To Objects-加入空列表

[英]LINQ To Objects - Join on empty list

我有三個分別來自不同數據源的列表,現在我想將它們加入LINQ語句中以獲得最終結果。 但是,列表之一完全有可能是空的。 如果列表為空,看來我不能只寫一個LINQ語句來加入其中的任何一個。

我已經編寫了以下測試。 我得到的錯誤是“ NullReferenceException:對象引用未設置為對象的實例。”

// these colloections emulate the table structures and data
List<Agent> agents = new List<Agent>();
agents.Add(new Agent { AgentId = 1, ClientId = 11 });
agents.Add(new Agent { AgentId = 1, ClientId = 12 });
agents.Add(new Agent { AgentId = 1, ClientId = 13 });
agents.Add(new Agent { AgentId = 1, ClientId = 14 });
agents.Add(new Agent { AgentId = 2, ClientId = 21 });
agents.Add(new Agent { AgentId = 2, ClientId = 22 });
agents.Add(new Agent { AgentId = 3, ClientId = 31 });

List<Client> clients = new List<Client>();
clients.Add(new Client { ClientId = 11, ClientName = "A Client 11", Status = "A" });
clients.Add(new Client { ClientId = 12, ClientName = "A Client 12", Status = "A" });
clients.Add(new Client { ClientId = 13, ClientName = "A Client 13", Status = "A" });
clients.Add(new Client { ClientId = 14, ClientName = "A Client 14", Status = "A" });
clients.Add(new Client { ClientId = 21, ClientName = "A Client 21", Status = "A" });
clients.Add(new Client { ClientId = 22, ClientName = "A Client 22", Status = "A" });
clients.Add(new Client { ClientId = 31, ClientName = "A Client 31", Status = "A" });

// Upon initilization, there are no records here.  Eventually, this "table" will be populated, 
// but only after the user has used the app for a while. If I populate this list, it works assuming
// the agent ID I'm looking for is in the collection.  But if it's not the join fails
List<ClientAdminFee> adminFees = new List<ClientAdminFee>();
//  adminFees.Add(new ClientAdminFee { AgentId = 1, ClientId = 11, AdminFee = 0.05m, EffectiveFrom = DateTime.Parse("2017-01-01") });
//  adminFees.Add(new ClientAdminFee { AgentId = 1, ClientId = 12, AdminFee = 0.05m, EffectiveFrom = DateTime.Parse("2017-01-01") });
//  adminFees.Add(new ClientAdminFee { AgentId = 1, ClientId = 13, AdminFee = 0.05m, EffectiveFrom = DateTime.Parse("2017-01-01") });
//  adminFees.Add(new ClientAdminFee { AgentId = 1, ClientId = 14, AdminFee = 0.05m, EffectiveFrom = DateTime.Parse("2017-01-01") });

var thisAgent = 1;
var theseAgents = agents.Where(x => x.AgentId == thisAgent).ToList();
var theseClients = clients.ToList();
var theseAdminFees = adminFees.Where(x => x.AgentId == thisAgent).ToList();

var final = (from ar in theseAgents
             join c in theseClients on ar.ClientId equals c.ClientId
             join caf in theseAdminFees on new { ar.AgentId, c.ClientId } equals new { caf.AgentId, caf.ClientId } into d
             from caf in d.DefaultIfEmpty()
             select new ClientWithAdminFee
             {
                 AgentId = Convert.ToInt32(ar.AgentId),
                 ClientId = Convert.ToInt32(c.ClientId),
                 ClientName = c.ClientName,
                 Status = c.Status,
                 AdminFee = caf.AdminFee ?? 0.00m,
                 EffectiveDate = caf.EffectiveFrom ?? DateTime.Now
             }).ToList();

final.Dump();

就像我說的,如果我取消注釋adminFees條目,並搜索AgentId 1,那么它將起作用。 但是,如果我搜索不在該集合中的代理,則根本無法進行加入。 因此,我如何編寫此代碼,以便在有條目以及沒有條目的情況下都能正常工作。

嘗試這個

var final = (from ar in theseAgents
                         join c in theseClients on ar.ClientId equals c.ClientId
                         join caf in theseAdminFees on new { ar.AgentId, c.ClientId } equals new { caf.AgentId, caf.ClientId } into d
                         from caf in d.DefaultIfEmpty()
                         select new ClientWithAdminFee
                         {
                             AgentId = Convert.ToInt32(ar.AgentId),
                             ClientId = Convert.ToInt32(c.ClientId),
                             ClientName = c.ClientName,
                             Status = c.Status,
                             AdminFee = caf != null ? (caf.AdminFee) : 0,
                             EffectiveDate = caf != null ? (caf.EffectiveFrom == null ? DateTime.Now : caf.EffectiveFrom) : DateTime.Now,
                         }).ToList();

暫無
暫無

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

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