Linq to sql 使用linq2sql将DateTime和int字段转换为字符串
我正在尝试构建一个linq来从sql server中的一个表中获取值,该表将两个不同格式的字段DateTime和Int合并为一个字符串。即:Linq to sql 使用linq2sql将DateTime和int字段转换为字符串,linq-to-sql,concatenation,Linq To Sql,Concatenation,我正在尝试构建一个linq来从sql server中的一个表中获取值,该表将两个不同格式的字段DateTime和Int合并为一个字符串。即: var result = from a in db.tbTable select new { a.field1, Description = a.DateTimeField.Value.ToShortDateString() + " - " + a.Cod_filed }; 另外,如果其中一个或两个字段(DateTime和Int)为空?我还需要做什么吗
var result = from a in db.tbTable select new { a.field1, Description = a.DateTimeField.Value.ToShortDateString() + " - " + a.Cod_filed };
另外,如果其中一个或两个字段(DateTime和Int)为空?我还需要做什么吗
Thanx to all如果
datetime字段
和Cod\u字段
都可以为空
,那么您可以尝试
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
};
但是,如果我在数据库中定义这些字段可以为null,我可以使用一些三元if来检查它,而不是仅在两个字段都不为null时尝试concat吗?即使两个字段都不为空,我也会出错。我的意思是它们有值。即使这样也不行:
var result=from a in db.tbTable select new{a.field1,Description=a.DateTimeField.HasValue?a.DateTimeField.value.ToSortDateString():string.Empty}
错误:无法转换expression@Varois“这不起作用”到底是什么意思?您可以发布异常+异常表吗?无法转换表达式“Table”(tbLoteAlojado)。其中(a=>(a.Data\u Alojamento!=null))。选择(a=>new f\u AnonymousTyped82(Num\u Lote\u Alojado=a.Num\u Lote\u Alojado,descripao=IIF((convert(a.Data\u Alojamento.Value)!=null),a.Data\u Alojamento.Value.ToShortDateString()),调用(Value)(System.Func
1[System.String]()()()()()))"""""""""""""。。