Click here to Skip to main content
15,885,000 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello sir/madam

I would like to filter a datatable.
For that I am using LINQ in this way its not working its give an error
"The source contains no DataRows."


First i get a value in a datatable dt.
now i am using LINQ
C#
var query = from r in dt.AsEnumerable()
               where r.Field<string>("AI_No_Of_Conception") == ""
                     select r;
       DataTable   dtLink=new DataTable();
               dtLink = query.CopyToDataTable();

If any another option how to apply a query on datatable and bind it another Datatable then please suggest my.
Posted
Updated 29-Nov-12 1:12am
v2

Hi
Try this out,

C#
var query = from r in dt.AsEnumerable() where 
String.Equals(r.Field<string>("AI_No_Of_Conception"),"")                             
select r;
// any one please suggest an idea to optimize the following two lines
if(result.Count() >0)// to avoid exception "The source contains no DataRows"
            dtLink = result.CopyToDataTable();


--SJ
 
Share this answer
 
v3
Comments
Rohit Sharma706 29-Nov-12 23:55pm    
thnx for rply.
its work to get the value on select query but when bind this value to datatable (dtLink = query.CopyToDataTable();) then i get an error "The source contains no DataRows."
so if you have any idea how to bind it into datatable then plz suggest me.
codeninja-C# 30-Nov-12 0:10am    
Hi,
I updated my result and it will be working fine, but find the better way to avoid that exception. Once you find let me know
--SJ
I faced the same issue some time back and figured out that the rootcause is very silly you need to use linq query like this

C#
var query = from r in dt.AsEnumerable()
where r.Field("AI_No_Of_Conception").ToString() == ""
select r;


just use Tostring()
 
Share this answer
 
Comments
Rohit Sharma706 29-Nov-12 7:39am    
thx for rply.
but sorry to say that your suggestion is not working because its argument exception over Field<t>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900