我正在LINQ中的两个数据表之间进行正确的连接。我在orderby行收到错误""指定演员表无效"。
" SomeOtherID" column在dbml中的类型为System.Int64,并允许DBNull。该列在数据中有一些空值,这是有效的。似乎这些空值必须在orderby语句中以某种方式处理,但我不确定如何。数据通过Web服务传入。我检查了reference.cs文件,该列的相应属性是一个int。
LINQ语句应该如何?
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
{
......
});
答案 0 :(得分:3)
同时检查:LINQ: OrderBy with nullable columns in TypedDataSets
尝试以下方式
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
{
......
});