tags:

views:

41

answers:

4

i'm trying to get an error description according to error Id:

String errorDesc = from resultCodesTableRow in resultCodesDT.AsEnumerable()
                            where resultCodesTableRow.Field<int>("Error_Code_Column_Name") == errorCode
                            select resultCodesTableRow.Field<string>("Error_Desc_Column_Name").ToString();

why do i get the error:

"Cannot implicitly convert type 'System.Data.EnumerableRowCollection' to 'string'" ?

how does the query supposed to look ?

A: 

Change resultCodesDT.AsEnumerable() to resultCodesDT.rows

Does this work:

where (int)resultCodesTableRow.GetItem("Error_Code_Column_Name") == errorCode
select resultCodesTableRow.GetItem("Error_Desc_Column_Name")
Hogan
no , there is no "GetItem" function in resultCodesTableRow.
Rodniko
A: 

I think you meant resultCodesDT.Tables['XXX'].AsEnumerable()

vc 74
A: 

I am not pretty sure what the actual LINQ query is returning most probably by seeing it and as you said you are getting error it might be returning a Collection so in order to avoid use first or last method to the query ie

String errorDesc = from resultCodesTableRow in resultCodesDT.AsEnumerable() where resultCodesTableRow.Field("Error_Code_Column_Name") == errorCode select resultCodesTableRow.Field("Error_Desc_Column_Name").First().ToString();

Maxymus
A: 

the Select function in your statement will return an IEnumerable. instead you need to use something like

String errorDesc = (from resultCodesTableRow in resultCodesDT.AsEnumerable()
                            where resultCodesTableRow.Field<int>("Error_Code_Column_Name") == errorCode
                            select resultCodesTableRow.Field<string>("Error_Desc_Column_Name").ToString()).First();

or maybe this would work but is untested

 String errorDesc = resultCodesDT.Where(X=> x.Error_Code_Column_Name==errorCode)
.Select(s=>s.Error_Desc_Column_Name.toString()).First();
Si Robinson