C# LINQ replacing nulls with meaningful string

大憨熊 提交于 2020-08-08 03:56:32

问题


From the list

class Delivery
{
    public string ProductCode
    {
        get;
        set;
    }

    public DateTime? OrderedDate
    {
        get;
        set;
    }

    public DateTime? DeliveryDate
    {
        get;
        set;
    }

    public Delivery(string pcode, DateTime? orddate, DateTime? deldate)
    {
        ProductCode = pcode;
        OrderedDate = orddate;
        DeliveryDate = deldate;
    }
}


List<Delivery> DeliveryList = new List<Delivery>();
DeliveryList.Add(new Delivery("P001",new DateTime(2009,01,27),null));
DeliveryList.Add(new Delivery("P007",new DateTime(2009,05,17),null));
DeliveryList.Add(new Delivery("P031", new DateTime(2008, 03, 15),
new DateTime(2008,04 ,22)));
DeliveryList.Add(new Delivery("P011",new DateTime(2009,01,27),
new DateTime(2009,02,12)));
DeliveryList.Add(new Delivery("P041",new DateTime(2009,01,27),null));
DeliveryList.Add(new Delivery("P051", new DateTime(2009, 01, 27),
new DateTime(2009, 02, 12)));
DeliveryList.Add(new Delivery("P501",new DateTime(2009,01,27),null));
DeliveryList.Add(new Delivery("P801",new DateTime(2009,01,27),null));

var query = DeliveryList.OrderBy(p => p.DeliveryDate);

For Report purpose ,During LINQ execution,What is the way to replace null values (Based on Delivery Date) with message "Yet to be delivered" (DateTime is value type).


回答1:


var result = DeliveryList.Select(x => new
{
    ProductCode = x.ProductCode,
    OrderedDate = x.OrderedDate,
    DeliveryDate = x.DeliveryDate.HasValue 
        ? x.DeliveryDate.Value.ToString() : "Yet to be delivered"
}).OrderBy(p => p.DeliveryDate).ToArray();



回答2:


I'm not 100% sure what your asking but it sounds like you want to convert DeliverList into a collection of strings indicating when they were delivered. In the case of a null DeliveryDate though you want the string "Yet to be delivered". If so try the following.

var dates = DeliveryList
  .Select(x => x.DeliverDate 
     ? x.DeliverDate.Value.ToString 
     : "Yet to be delivered");



回答3:


Darin's solution is neat and I'd go with it. As an alternate consideration...

if you want to keep the type as with the solution above an anonymous type is created and the delivery date will probably end up as a string.

List<Delivery> query = (from d in DeliveryList
                        select new Delivery
                        (
                              d.ProductCode, 
                              d.OrderedDate, 
                              d.DeliveryDate ?? DateTime.Now
                        )).OrderBy(p=>p.DeliveryDate).ToList();

if you had an empty constructor in your Delivery class you could do something like

List<Delivery> query2 = (from d in DeliveryList
                         select new Delivery
                         {
                             DeliveryDate = d.DeliveryDate ?? DateTime.Now,
                             OrderedDate = d.OrderedDate,
                             ProductCode = d.ProductCode
                          }).OrderBy(p=>p.DeliveryDate).ToList();

The one thing you'd have to do then, is have a meaninful replacement for your DeliveryDate if it's null. I don't think DateTime.Now would be useful and now you'd be stuck with a DateTime field. The advantage obviously, is that you're sitting with a List object which is strongly cast. And helps I guess if later on you do put logic into your contructor.



来源:https://stackoverflow.com/questions/1838232/c-sharp-linq-replacing-nulls-with-meaningful-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!