开发者

Concat DateTime and int field to string using linq2sql

开发者 https://www.devze.com 2023-03-16 12:26 出处:网络
I am try to build a linq to get values from a table in sql server that concats 2 fields in different format, DateTime and Int, into one string. Ie.:

I am try to build a linq to get values from a table in sql server that concats 2 fields in different format, DateTime and Int, into one string. Ie.:

var result = from a in db.tbTable select new { a.field1, Description =  a.DateTimeField.Value.ToShortDateString() + " - " + a.Cod_filed };

Also, if one of those, or both, of the fields (DateTime and Int) are null? Do i need to do anything else?

Thanx to开发者_JS百科 all


If both DateTimeField and Cod_filed are Nullable then you can try

var result = from a in db.tbTable 
            select new 
                  { 
                       a.field1, 
                       Description =  a.DateTimeField.HasValue  
                                        ? a.DateTimeField.Value.ToShortDateString() 
                                        : string.Empty + " - " 
                                      + a.Cod_filed ?? string.Empty 
                  };
0

精彩评论

暂无评论...
验证码 换一张
取 消