-2

I am trying to avoid the for loop and use LINQ instead of for loop. Any help will be highly appreciated. I have been trying to use dt.select (a=>...) but it is always throwing error "Cannot convert lambda expression to type string because it is not delegate type".

    AutoCompleteStringCollection coll = new AutoCompleteStringCollection();  
    cn = new SqlConnection("Data Source=.;Initial Catalog=XYX;Integrated 
    Security=True");
    da = new SqlDataAdapter("select UserName from ServiceProviders order 
    by UserName asc", cn);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
      {
         for (int i = 0; i < dt.Rows.Count; i++)
            {
             coll.Add(dt.Rows[i]["UserName"].ToString());
            }
        }
Gaurav
  • 623
  • 5
  • 11
  • So you want to loop over every row and add the username field to the coll collection? You want to do a loop without a loop? – Hogan Dec 09 '18 at 22:42
  • I guess that here is a tip which you need https://stackoverflow.com/questions/4974159/convert-datarowcollection-to-ienumerablet/4974176 – jkosmala Dec 09 '18 at 22:48
  • LINQ would only make sense here if `coll` is of type List<>, because of its AddRange method taking an IEnumerable which is the basic data structure of LINQ. In this case your loop (and the if block around) can be replaced by `coll.AddRange(dt.Rows.Select(row => row["UserName"].ToString()));`. But like Hogan said under the hood the same happens like in his solution. – ckuri Dec 09 '18 at 23:01
  • getting error that dt,rows does not contains definition of Select. – Gaurav Dec 09 '18 at 23:07

2 Answers2

0

I think you just want an easy way to do this loop, I'd use foreach instead of if and for. The nice thing about foreach is that you don't have to check count. If there are none it won't execute. The code should look something like this:

foreach( var row in dt.Rows)
{
   coll.Add(row["Username"].ToString());
}

Remember this -- Linq is not about about removing flow of control (loops) from your code -- the loop still exists. Linq is about expressions and expressions that can include loops or flow of control. (I know, it is subtle).

Bottom line -- if you need a loop, you need a loop. Linq does not "remove" that need.

Hogan
  • 69,564
  • 10
  • 76
  • 117
  • Thanks! Getting an error with it. Can;t apply indexing with []. – Gaurav Dec 09 '18 at 23:04
  • @Gaurav -- can I see your exact code? In the loop in debugging row should be of type DataRow, if not then you have a typo. – Hogan Dec 10 '18 at 15:15
0

To do a linq query against a DataTable you need to call .AsEnumerable() on it first. I notice your coll is not a List<string> but you're adding your selection to it as a string, you may want to look into that, but to answer your specific question, here is a basic example that queries against a DataTable and adds specific rows to a List<T>.

var dt = new DataTable();
dt.Columns.Add("UserName");
dt.Columns.Add("Bar");
dt.Rows.Add("Bob", 1);
dt.Rows.Add("Bill", 2);
dt.Rows.Add("John", 3);

var foo = dt.AsEnumerable().Select(r => r["UserName"]).ToList();
var bar = dt.AsEnumerable().Select(r => r["Bar"]).ToList();
gilliduck
  • 2,762
  • 2
  • 16
  • 32