"指定的转换是无效"在LINQ的排序依据子句中的错误 [英] "Specified cast is not valid" error in LINQ's orderby clause
问题描述
我做了正确的LINQ两个DataTable之间的连接。我在排序依据行指定的转换无效。
得到一个错误SomeOtherID列的类型是System.Int64在DBML,并允许为DBNull。该列中的数据,这是有效的一些空值。这似乎以某种方式在排序依据语句来处理这些空值有,但我不知道怎么样。的数据即将在通过Web服务。我检查了reference.cs文件和列对应的属性是int。
应该如何LINQ的语句是什么?
VAR的查询=(从表1中DataTable1.AsEnumerable()
加入表2中DataTable2.AsEnumerable()
在(INT)表1 [客户ID]等于(INT)表2 [客户ID]到外
从表2中outer.DefaultIfEmpty()
排序依据(INT?)表2 [SomeOtherID]
选择新
{
......
});
另外,请检查:的 LINQ:排序依据与TypedDataSets 空列
试试下面的方法
VAR的查询=
(从表1中DataTable1.AsEnumerable()
加入表2中DataTable2.AsEnumerable()
在(INT)表1 [客户ID]等于(INT)表2 [客户ID]
成outer.DefaultIfEmpty从表2的外()
// order by子句此处更改
排序依据
(Convert.IsDBNull(表2 [SomeOtherID])0:(INT)?
Convert.ToInt32(表2 [SomeOtherID]))
选择新
{
......
});
I am doing a right join between two datatables in LINQ. I am getting an error at the orderby line ""Specified cast is not valid".
"SomeOtherID" column is of type System.Int64 in the dbml and allows DBNull. The column has some null values in the data, which is valid. It seems these nulls have to be handled in a certain way in the orderby statement but I am not sure how. The data is coming in through a web service. I checked the reference.cs file and the corresponding property for the column is an int.
How should the LINQ statement be?
var query = (from table1 in DataTable1.AsEnumerable()
join table2 in DataTable2.AsEnumerable()
on (int) table1["CustomerID"] equals (int) table2["CustomerID"] into outer
from table2 in outer.DefaultIfEmpty()
orderby (int?)table2["SomeOtherID"]
select new
{
......
});
Also check : LINQ: OrderBy with nullable columns in TypedDataSets
try below way
var query =
(from table1 in DataTable1.AsEnumerable()
join table2 in DataTable2.AsEnumerable()
on (int) table1["CustomerID"] equals (int) table2["CustomerID"]
into outer from table2 in outer.DefaultIfEmpty()
//order by clause changed here
orderby
(Convert.IsDBNull(table2["SomeOtherID"]) ? 0 : (int?)
Convert.ToInt32(table2["SomeOtherID"]))
select new
{
......
});
这篇关于"指定的转换是无效"在LINQ的排序依据子句中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!