C # LINQ replacing zeros with a meaningful string

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 the purpose of the report, at runtime LINQ, what is the way to replace null values ​​(based on delivery date) with the message "Still to be delivered" (DateTime - value type).

+3
source share
3 answers
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();
+7
source

I'm not 100% sure what you are asking for, but it looks like you want to convert a DeliverList to a collection of strings indicating when they were delivered. In the case of null DeliveryDate, although you want the string “Still to be delivered”. If so, try the following.

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

, . ...

, , , , .

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

, -

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 only thing you had to do was have an average replacement for your DeliveryDate if it is zero. I do not think that DateTime.Now will be useful, and now you will loop into the DateTime field. The advantage, obviously, is that you are sitting with a List object, which is very different. And it helps, I think, if you later put the logic in your constructor.

0
source

Source: https://habr.com/ru/post/1724591/


All Articles