【问题标题】:How to handle null tables in linq left join如何处理 linq 左连接中的空表
【发布时间】:2018-01-18 07:32:43
【问题描述】:

我正在编写如下的 linq 查询:-

from eventDetails in CurrentDataContext.EventCarriers
join eventlog in CurrentDataContext.EventLogCarriers on eventDetails.EventID equals eventlog.EventId into hhh
from eventlog in hhh.DefaultIfEmpty()
join quiz in CurrentDataContext.QuizCarriers on eventDetails.EventID equals competition.EventId into aaa
from quiz in aaa.DefaultIfEmpty()

where eventDetails.EventID == eventId && eventDetails.EventStatusId == 1 && eventlog.CreatedBy == userId                                      
group new { eventDetails, quiz, eventlog } by new
{
  eventDetails.EventID,
  eventDetails.Name                                        
} into g
select new VenueModel
{
  Name = g.Key.Name,
  EventId = g.Key.EventID,
  StartDate = g.Key.StartDate,
  EndDate = g.Key.EndDate,
  EventDescr = g.Key.EventDescr,
  EventIconPath = g.Key.EventIconPath,
  EventImagePath = g.Key.EventImagePath,
  Prizes = (from a in g.Select(a => a.competitions)
                  group a by new
                  {
                      a.PrizeId,
                      a.PrizeIconPath,
                      a.CompetitionDescr,
                      a.PrizeImagePath,
                      a.EndDate,
                      a.StartDate,
                      a.Name,
                      a.EventId,
                      a.StatusId
                  } into x

                  select new CCompetitionModel
                  {
                      PrizeDescrption = x.Key.PrizeDescr,
                      PrizeIconPath = x.Key.PrizeIconPath,
                      PrizeId = x.Key.PrizeId,                                                              
                  }).ToList(),
}).FirstOrDefault();

但我收到类似 转换为值类型“System.Int32”的错误,因为具体化值为 null。结果类型的泛型参数或查询必须使用可为空的类型。

【问题讨论】:

    标签: c# linq linq-to-sql


    【解决方案1】:
    //Declare your class property nullable.
    //For example, if you are getting start date null from database, then your property will be like following.
    
    
        public class Event
        {
          public DateTime? StartDate{get; set;}
          //add other properties of this class here..
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多