【问题标题】:how do i handle value when the value is null or Empty using linq query当值为 null 或 Empty 时如何使用 linq 查询处理值
【发布时间】:2020-04-25 15:55:33
【问题描述】:

我有这个代码返回所有带有回复信息的票,所以当 Ticket_Reply 中没有状态值时,我需要显示“XXX”

enter    var query = (from st in Db.Support_Teckets
                     join pr in Db.Technical_problem on st.Technical_problem_Id equals pr.Technical_problem_Id
                     join x in 

                     from rp in Db.Ticket_Reply.GroupBy(m =>
       m.Support_Tecket_Id).Select(m => m.OrderByDescending(x=>x.Date).FirstOrDefault())
                     join tr in Db.trainers on rp.trainer_id equals tr.trainer_id
                     select new {rp,tr}


       on st.Support_Tecket_Id equals x.rp.Support_Tecket_Id into g

       from gx in g.DefaultIfEmpty()



                select new SupportTicketsDetails
            {

                Status = gx.rp.Status,



                }).ToList().OrderByDescending(a => a.Created_Date);

感谢您的合作

【问题讨论】:

    标签: c# asp.net asp.net-mvc linq asp.net-core


    【解决方案1】:

    假设 gx.rp.Status 是一个字符串尝试使用三元条件运算符,例如

    Status = string.IsNullOrWhiteSpace(gx.rp.Status) ? "XXX":gx.rp.Status
    

    enter    var query = (from st in Db.Support_Teckets
                     join pr in Db.Technical_problem on st.Technical_problem_Id equals pr.Technical_problem_Id
                     join x in 
    
                     from rp in Db.Ticket_Reply.GroupBy(m =>
       m.Support_Tecket_Id).Select(m => m.OrderByDescending(x=>x.Date).FirstOrDefault())
                     join tr in Db.trainers on rp.trainer_id equals tr.trainer_id
                     select new {rp,tr}
    
    
       on st.Support_Tecket_Id equals x.rp.Support_Tecket_Id into g
    
       from gx in g.DefaultIfEmpty()
    
    
    
                select new SupportTicketsDetails
            {
    
                Status = string.IsNullOrWhiteSpace(gx.rp.Status) ? "XXX":gx.rp.Status,
    
    
    
                }).ToList().OrderByDescending(a => a.Created_Date);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-28
      • 2022-11-15
      • 2021-06-27
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多