---
title: "Specific cast is not valid in linq query when compare two tables"  
description: "Specific cast is not valid in linq query when compare two tables"  
author: "???? ???????"  
published: 2018-04-25  
updated: 2018-04-25  
canonical: https://www.mindstick.com/forum/34476/specific-cast-is-not-valid-in-linq-query-when-compare-two-tables  
category: "c#"  
tags: ["c#", "ado.net", ".net"]  
reading_time: 2 minutes  

---

# Specific cast is not valid in linq query when compare two tables

Specific cast is not valid in [linq query](https://www.mindstick.com/forum/33908/how-to-select-and-retrieve-data-using-linq-query-in-entity-framework) when [compare two](https://www.mindstick.com/forum/23112/how-to-compare-two-arraylists-of-type-arraylist-string) tables\
Problem\
\
Error display in linq query "specific cast is not valid" at System.Data.DataRowExtensions.UnboxT`1.ValueField(Object value) at System.Data.DataRowExtensions.Field[T](DataRow row, String columnName)\
\
LinqQuery give error\
\
\
\
var query1 = (from x in table1.AsEnumerable()\
join y in table2.AsEnumerable() on x.Field<int>("UnitCode") equals y.Field<int>("UnitCode")\
where y.Field<decimal>("CurrentMeterReading") > x.Field<decimal>("CurrentMeterReading")\
select new { UnitCode = x.Field<int>("UnitCode"), CurrentReading = x.Field<decimal>("CurrentMeterReading") }).ToList(); \
\
\
Details\
\
When make debug the first data table tableReadingExcelsheet [retrieve data](https://www.mindstick.com/forum/160347/how-do-you-retrieve-data-from-a-nosql-database-like-mongodb) from [excel sheet](https://www.mindstick.com/forum/23040/to-import-excel-sheet-into-sql-database) .\
\
second datatable readingfromInvoiceTablesql retrieve data from wahinvoice [table in sql](https://www.mindstick.com/forum/205/find-the-all-column-with-schema-for-any-table-in-sql-server) .\
\
I need to get list of rows in excel sheet that have current reading less than\
\
currentreading in wahinvoice table for same UnitCode then display in [datagridview](https://www.mindstick.com/articles/607/working-with-datagridview-in-vc-sharp) .\
\
\
\
\
private void button2_Click(object sender, EventArgs e)\
{\
DataTable tableReadingExcelsheet = new DataTable();\
tableReadingExcelsheet.Columns.AddRange(new DataColumn[] { new DataColumn("UnitCode", typeof(int)), new DataColumn("CurrentMeterReading", typeof(decimal)) });\
tableReadingExcelsheet = ShowdataFromExcel();\
DataTable readingfromInvoiceTablesql = new DataTable();\
readingfromInvoiceTablesql.Columns.AddRange(new DataColumn[] { new DataColumn("Serial", typeof(int)), new DataColumn("UnitCode", typeof(int)), new DataColumn("CurrentMeterReading", typeof(decimal)) });\
readingfromInvoiceTablesql = GetCurrentReadingUnitCodesql();\
var query1 = (from x in tableReadingExcelsheet.AsEnumerable()\
join y in readingfromInvoiceTablesql.AsEnumerable() on x.Field<int>("UnitCode") equals y.Field<int>("UnitCode")\
where y.Field<decimal>("CurrentMeterReading") > x.Field<decimal>("CurrentMeterReading")\
select new { UnitCode = x.Field<int>("UnitCode"), CurrentReading = x.Field<decimal>("CurrentMeterReading") }).ToList();\
\
dataGridView1.DataSource = query1;\
dataGridView1.Refresh();\
\
}\
//get data from excel success\
public System.Data.DataTable ShowdataFromExcel()\
{\
string connectionString = [string.Format](https://www.mindstick.com/forum/1763/string-format-with-null-values-c-sharp)("Provider=Microsoft.ACE.OLEDB.12.0;[Data Source](https://www.mindstick.com/interview/808/what-is-a-data-source)={0};Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";", txtpath.Text);\
\
OleDbConnection con = new OleDbConnection(connectionString);\
\
\
con.Open();\
\
string str = @"SELECT [??? ?????????] as [UnitCode],[????? ??????]as[CurrentMeterReading] FROM [Sheet5$] ";\
OleDbCommand com = new OleDbCommand();\
com = new OleDbCommand(str, con);\
OleDbDataAdapter oledbda = new OleDbDataAdapter();\
oledbda = new OleDbDataAdapter(com);\
DataSet ds = new DataSet();\
ds = new DataSet();\
oledbda.Fill(ds, "[Sheet5$]");\
con.Close();\
System.Data.DataTable dt = new System.Data.DataTable();\
dt = ds.Tables["[Sheet5$]"];\
return dt;\
\
\
}\
//get [data from sql](https://www.mindstick.com/forum/12885/how-to-retrieving-data-from-sql-server-and-adding-it-to-arraylist) wahinvoice success\
public System.Data.DataTable GetCurrentReadingUnitCodesql()\
{\
sqlquery = @"select Serial,UnitCode, CurrentMeterReading\
from( select Serial,UnitCode, CurrentMeterReading, ROW_NUMBER() OVER(PARTITION BY UnitCode ORDER BY Serial desc) as rn\
from WAHInvoice) as a\
where rn = 1";\
\
\
System.Data.DataTable tbCurrentReading = DataAccess.ExecuteDataTable(sqlquery);\
return tbCurrentReading;\
} \


---

Original Source: https://www.mindstick.com/forum/34476/specific-cast-is-not-valid-in-linq-query-when-compare-two-tables

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
