我一直在使用這個linq查詢一段時間。我看了很多關於stackoverflow和以後的答案。我已經嘗試了很多解決方案並閱讀了很多。下面的代碼是我無數次嘗試建立這種內在的2加入,我得到的錯誤是Linq左外部連接C#
對象引用未設置到對象的實例
嘗試
var records = from cr in lstContactResponse
join jn in lstJourneyNodeData on cr.GrandparentId equals jn.Id into a
from x in a.DefaultIfEmpty()
join j in lstJourney on x.JourneyId equals j.Id into b
from y in b.DefaultIfEmpty()
join ce in lstCampaignElement on y.Name equals ce.LinkedJourney into c
from z in c.DefaultIfEmpty()
join c in lstCampaign on z.CampaignId equals c.Id into d
from w in d.DefaultIfEmpty()
select new JourneyRecord
{
CompanyName = w.Company,
BrandName = w.Brand,
CampaignName = w.Name,
Channel = z.Type,
Wave = "",
CampaignId = w.Id,
ActivityDate = cr.ResponseDate,
Activity = "",
Unsubscribed = cr.Unsubscribed,
Responded = cr.Responded,
Clicked = cr.Clicked,
Viewed = cr.Viewed,
Sent = cr.Sent,
Targeted = cr.Targeted,
HardBounced = cr.HardBounced,
SoftBounced = cr.SoftBounced,
WasTargeted = cr.WasTargeted,
Email = "",
Id = "",
CampaignElementId = z.Id,
CampaignWaveId = "J" + x.Id,
ContactId = cr.ContactId,
AtTaskId = w.AtTaskId,
LinkClicked = cr.Referrer,
OptTopic = z.TopicId,
DiseaseState = "",
ElementDescription = y.Name,
WaveDescription = x.Label
};
嘗試乙
var records = from cr in lstContactResponse
join jn in lstJourneyNodeData on cr.GrandparentId equals jn.Id into a
from x in a.DefaultIfEmpty()
join j in lstJourney on x.JourneyId equals j.Id into b
from y in b.DefaultIfEmpty()
join ce in lstCampaignElement on y.Name equals ce.LinkedJourney into c
from z in c.DefaultIfEmpty()
join c in lstCampaign on z.CampaignId equals c.Id into d
from w in d.DefaultIfEmpty()
select new JourneyRecord
{
CompanyName = x == null ? null : w.Company,
BrandName = x == null ? null : w.Brand,
CampaignName = x == null ? null : w.Name,
Channel = x == null ? null : z.Type,
Wave = "",
CampaignId = x == null ? null : w.Id,
ActivityDate = x == null ? null : cr.ResponseDate,
Activity = "",
Unsubscribed = x == null ? null : cr.Unsubscribed,
Responded = x == null ? null : cr.Responded,
Clicked = x == null ? null : cr.Clicked,
Viewed = x == null ? null : cr.Viewed,
Sent = x == null ? null : cr.Sent,
Targeted = x == null ? null : cr.Targeted,
HardBounced = x == null ? null : cr.HardBounced,
SoftBounced = x == null ? null : cr.SoftBounced,
WasTargeted = x == null ? null : cr.WasTargeted,
Email = "",
Id = "",
CampaignElementId = x == null ? null : z.Id,
CampaignWaveId = "J" + (x == null ? null : x.Id),
ContactId = x == null ? null : cr.ContactId,
AtTaskId = x == null ? null : w.AtTaskId,
LinkClicked = x == null ? null : cr.Referrer,
OptTopic = x == null ? null : z.TopicId,
DiseaseState = "",
ElementDescription = x == null ? null : y.Name,
WaveDescription = x == null ? null : x.Label
};
嘗試ç
var records = from cr in lstContactResponse
join jn in lstJourneyNodeData on cr.GrandparentId equals jn.Id into a
from x in a.DefaultIfEmpty()
join j in lstJourney on x.JourneyId equals j.Id into b
from y in b.DefaultIfEmpty()
join ce in lstCampaignElement on y.Name equals ce.LinkedJourney into c
from z in c.DefaultIfEmpty()
join c in lstCampaign on z.CampaignId equals c.Id into d
from w in d.DefaultIfEmpty()
select new JourneyRecord
{
CompanyName = w == null ? null : w.Company,
BrandName = w == null ? null : w.Brand,
CampaignName = w == null ? null : w.Name,
Channel = z == null ? null : z.Type,
Wave = "",
CampaignId = w == null ? null : w.Id,
ActivityDate = cr == null ? null : cr.ResponseDate,
Activity = "",
Unsubscribed = cr == null ? null : cr.Unsubscribed,
Responded = cr == null ? null : cr.Responded,
Clicked = cr == null ? null : cr.Clicked,
Viewed = cr == null ? null : cr.Viewed,
Sent = cr == null ? null : cr.Sent,
Targeted = cr == null ? null : cr.Targeted,
HardBounced = cr == null ? null : cr.HardBounced,
SoftBounced = cr == null ? null : cr.SoftBounced,
WasTargeted = cr == null ? null : cr.WasTargeted,
Email = "",
Id = "",
CampaignElementId = z == null ? null : z.Id,
CampaignWaveId = "J" + (x == null ? null : x.Id),
ContactId = cr == null ? null : cr.ContactId,
AtTaskId = w == null ? null : w.AtTaskId,
LinkClicked = cr == null ? null : cr.Referrer,
OptTopic = z == null ? null : z.TopicId,
DiseaseState = "",
ElementDescription = y == null ? null : y.Name,
WaveDescription = x == null ? null : x.Label
};
嘗試圍繞與.toList查詢()在做任何進一步的操作之前,還要檢查「記錄」的值 –
我在循環中這樣做,以便程序在沒有項目時不會崩潰。我甚至無法得到這個,這是查詢後的下一行。 'List lstRecords = null;如果(記錄!=空&& records.Count()> 0) { lstRecords = records.ToList(); }' –
coding
你檢查過你的所有列表值嗎? –