LINQ Choose from DataTable

Just started playing with datatable and LINQ today. I have a datatable that gets a list of names from an SQL database. I want to return a specific name from dt using LINQ.

I tried the following code without success. Is there something I'm doing wrong with the code.

dt returns a complete list of names, I just want to reduce the names to a single name. There is a name in the adventureworks database called Blade that I am trying to display only.

 DataTable dt =  DAL.GetNames();
      try
      {
          var q = from myrow in dt.AsEnumerable()
                  where myrow.Field<string>("Name") =="Blade"
                  select myrow;
          dataGridView1.DataSource = q;
      }

I tried replacing == with .equals. I am completely unfamiliar with the concept of using a language integrated query.

when I run the code noting that I am not getting any errors, etc., just no data returned.

+5
source share
1 answer

You define your request, but do not execute it.

Your line:

dataGridView1.DataSource = q;

Must be:

dataGridView1.DataSource = q.AsDataView();
+6
source

All Articles