LINQ order by 子句中出现“指定的转换无效”错误

2024-03-23

我正在 LINQ 中的两个数据表之间进行右连接。我在 orderby 行收到错误“指定的演员无效”。

“SomeOtherID”列在 dbml 中属于 System.Int64 类型,并允许 DBNull。该列的数据中有一些空值,这是有效的。似乎这些空值必须在 orderby 语句中以某种方式处理,但我不确定如何处理。数据通过网络服务传入。我检查了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
                                     {
                                         ......
                                     });

另请检查:LINQ:TypedDataSets 中具有可为空列的 OrderBy http://perezgb.com/2008/08/11/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
       {
           ......
       });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

LINQ order by 子句中出现“指定的转换无效”错误 的相关文章

随机推荐