Convert DataTable to LINQ: Cannot Query Multiple Fields

Importing a spreadsheet I populated a DataTable with this data and returned the expected results.

Trying to put this in a format, I can easily request a search for problem records. I did the following

public void Something(DataTable dt)
{
     var data = from row in dt.AsEnumerable()
                select row["Order"].ToString();
}

Works as expected, giving me a list of orders. However, I cannot add other fields to this EnumerableRowCollection. Trying to add other fields as follows gives me an error

public void Something(DataTable dt)
{
     // row["Version"] throws an error on me
     var data = from row in dt.AsEnumerable()
                select row["Order"].ToString(), row["Version"].ToString();
}

Error: "A local variable with the name" string "cannot be declared in this area, because it would give a different value to" row ", which is already used in the" child "area to donate something else"

I think I need an alias for the column name, but I'm out of luck. What am I missing here?

+3
3

, select. :

public void Something(DataTable dt)
{
    var data = from row in dt.AsEnumerable()
               select new { 
                            Order = row["Order"].ToString(), 
                            Something = row["Something"].ToString(),
                            Customer = row["Customer"].ToString(),
                            Address = row["Address"].ToString()
                          };
}

, . , data . , ( ).

+4

, select new, , :

var q = from o in db.Orders
        where o.Products.ProductName.StartsWith("Asset") && 
              o.PaymentApproved == true
        select new { name   = o.Contacts.FirstName + " " +
                              o.Contacts.LastName, 
                     product = o.Products.ProductName, 
                     version = o.Products.Version + 
                              (o.Products.SubVersion * 0.1)
                   };
+2

You probably need the following.

var data = from row
           in dt.AsEnumerable()
           select new { Order = row["Order"].ToString(), Version = row["Version"].ToString() };
0
source

Source: https://habr.com/ru/post/1756750/


All Articles