LINQ Query To Retrieve Field Value By Index

For retrieving data from many sources and formats, C# and VB.NET’s LINQ (Language Integrated Query) provides a consistent query syntax. Because it is incorporated into C# or VB, the incompatibility between programming languages and databases is removed. It also offers a unified querying interface for many types of data sources.

The LINQ Where filtering operator in C# will be covered in this article.

Where – Returns values from the collection based on a condtion.

using System;
using System.Linq;
using System.Collections.Generic;

//Table.Columns["ColumnName"].Ordinal;                                                          --find the DataTable's column's index.
//Table.AsEnumerable().ToList().Where(r => r.ItemArray.Contains(currentValue))                  --Filter the query based on condition 
//Table.AsEnumerable().ToList().Where(r => r.ItemArray.Contains(currentValue)).FirstOrDefault()   --Return the first element of a sequence or a default value if element isn't there

// Retrieve a table's currentValue 
var x= Table.AsEnumerable().ToList().Where(r => r.ItemArray.Contains(currentValue)).FirstOrDefault().ItemArray[Table.Columns["ColumnName"].Ordinal].ToString();


Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories