Click here to Skip to main content
12,881,367 members (29,485 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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
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 29-Nov-12 1:10am
Updated 29-Nov-12 1:12am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi

Try this out,

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
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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


just use Tostring()
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.170422.1 | Last Updated 30 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100