C#LINQ用有意义的字符串替换空值 [英] C# LINQ replacing nulls with meaningful string
本文介绍了C#LINQ用有意义的字符串替换空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
从列表中
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);
出于报告目的,在执行LINQ期间,用以下哪种方法替换空值(基于交货日期)?消息尚未交付"(DateTime是值类型).
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).
推荐答案
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();
这篇关于C#LINQ用有意义的字符串替换空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文