Can sombody please help me? I have datatble called DT with columns : kodId,kodO,RO,RD,Text and I need to select all Texts from rows with specific kodId and with with specific range between RO and RD. I have tried this query :
IEnumerable<string> results = from DataRow myRow in tempDT.Rows where myRow.Field<Int32>("kodId") == id && myRow.Field<Int32>("RO") <= Convert.ToInt32(rez) && myRow.Field<Int32>("RD") > Convert.ToInt32(rez) orderby myRow.Field<int>("kodO") select myRow.Field<string>("Text");
the variables have these values : id =1073754112 and rez="0"
and the datarow has these : kodId=1073754112, kodO=1, RO=0, RD=32767 Text ="<test/>"
but I am getting exception :Specified cast is not valid.
So maybe my syntax is bad, I don't know, because Linq is something new for me, so I woul be very happy if somebody has any advice
Well thank you mameenkhn , it helped me to move a bit, but the problem is not solved yet and I am getting the exception again, but not when executing the command, but aafter, when
I am trying to loop trought the results. And I have discovered this part :
It was the invalid cast again, but I have solved it few minutes ago and now I feel really stupid, because it was caused by bad sizes of integers. In the DB the fields are smallints so int16 but I used int32 in the query. But thank you to help me improve
my linq knowledge
Have a nice day
Marked as answer by medivo on Apr 08, 2012 10:51 AM
medivo
Member
48 Points
41 Posts
LINQ to Datatable
Apr 07, 2012 09:58 PM|LINK
Hi guys,
Can sombody please help me? I have datatble called DT with columns : kodId,kodO,RO,RD,Text and I need to select all Texts from rows with specific kodId and with with specific range between RO and RD. I have tried this query :
IEnumerable<string> results = from DataRow myRow in tempDT.Rows where myRow.Field<Int32>("kodId") == id && myRow.Field<Int32>("RO") <= Convert.ToInt32(rez) && myRow.Field<Int32>("RD") > Convert.ToInt32(rez) orderby myRow.Field<int>("kodO") select myRow.Field<string>("Text");
the variables have these values : id =1073754112 and rez="0"
and the datarow has these : kodId=1073754112, kodO=1, RO=0, RD=32767 Text ="<test/>"
but I am getting exception :Specified cast is not valid.
So maybe my syntax is bad, I don't know, because Linq is something new for me, so I woul be very happy if somebody has any advice
Linq DataTable
mameenkhn
Contributor
2026 Points
391 Posts
Re: LINQ to Datatable
Apr 08, 2012 03:38 AM|LINK
you have to use tempDT.AsEnumerable()
check it out
IEnumerable<string> results = from myRow in tempDT.AsEnumerable() where myRow.Field<Int32>("kodId") == id && myRow.Field<Int32>("RO") <= Convert.ToInt32(rez) && myRow.Field<Int32>("RD") > Convert.ToInt32(rez) orderby myRow.Field<int>("kodO") select myRow.Field<string>("Text");List<string> results = (from myRow in tempDT.AsEnumerable() where myRow.Field<Int32>("kodId") == id && myRow.Field<Int32>("RO") <= Convert.ToInt32(rez) && myRow.Field<Int32>("RD") > Convert.ToInt32(rez) orderby myRow.Field<int>("kodO") select myRow.Field<string>("Text")).ToList();Linq DataTable
--------------------------------------------------
Muhammad Amin
محمد امين
medivo
Member
48 Points
41 Posts
Re: LINQ to Datatable
Apr 08, 2012 10:31 AM|LINK
Well thank you mameenkhn , it helped me to move a bit, but the problem is not solved yet and I am getting the exception again, but not when executing the command, but aafter, when I am trying to loop trought the results. And I have discovered this part :
&& myRow.Field<Int32>("RO") <= Convert.ToInt32(rez) && myRow.Field<Int32>("RD") > Convert.ToInt32(rez) orderby myRow.Field<int>("kodO")as the source of my problem, because when I use only
IEnumerable<string> results = from myRow in tempDT.AsEnumerable() where myRow.Field<Int32>("kodId") == id select myRow.Field<string>("Text");it works perfectly, but I need the conditon to be in the query
mameenkhn
Contributor
2026 Points
391 Posts
Re: LINQ to Datatable
Apr 08, 2012 10:38 AM|LINK
What exception are you getting?
how are you looping through?
--------------------------------------------------
Muhammad Amin
محمد امين
medivo
Member
48 Points
41 Posts
Re: LINQ to Datatable
Apr 08, 2012 10:51 AM|LINK
It was the invalid cast again, but I have solved it few minutes ago and now I feel really stupid, because it was caused by bad sizes of integers. In the DB the fields are smallints so int16 but I used int32 in the query. But thank you to help me improve my linq knowledge
Have a nice day