Linq to SQL: DataTable.Rows [0] ["ColumnName"] equivalent

Consider this:

var query = from r in this._db.Recipes
            where r.RecipesID == recipeID
            select new { r.RecipesID, r.RecipesName };

How to get individual columns in my queryobject without using a for loop?

Basically: how to convert DataTable.Rows[0]["ColumnName"]to Linq syntax?

+1
source share
4 answers

Here's how to do it:

DataContext dc = new DataContext();

var recipe = (from r in dc.Recipes 
              where r.RecipesID == 1
              select r).FirstOrDefault();

if (recipe != null)
{
    id = recipe.RecipesID;
    name = recipe.RecipesName;
}
+1
source

It is not clear what you are looking for since your two samples are compatible.

As I can understand, you want:

var rows = query.ToList();
string name = rows[0].RecipesName;
+2
source
string name = this._db.Recipes.Single(r => r.RecipesID == recipeID).RecipesName;
+2

, . , ToList(), . , , , , :


query.First().ColumnName

:


var obj = query.FirstOrDefault();
if (obj != null)
   obj.ColumnName;

( ):

Linq to Datasets. - :


var query = from r in yourTable.AsEnumerable()
select r.Field<string>("ColumnName");
0

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


All Articles