Linq to Ado.net


Here i am trying to explain Linq using Ado.net

Code Snippet:

int result=1;

we are getting datable as result set as a dt.

result set

DataRow resultCodeRow = (from resultCodesTableRow in dt.AsEnumerable() where resultCodesTableRow.Field<int?>("Id").Equals(result) select resultCodesTableRow).FirstOrDefault();

In the code we have created object for the DataRow as a resultCodeRow and assigning values to the object.

(from resultCodesTableRow in dt.AsEnumerable() where resultCodesTableRow.Field<int?>("Id").Equals(result) select resultCodesTableRow).FirstOrDefault();

coming to Linq explaination


Here i am creating resultCodesTableRow as a alias name for the result set "dt" and dt as column name "id" and datatype integer,in front of integer it mentioned "?" the use this is when there is a null values in column we may get exception. To prevent exception we are using"?". after that we are comparing the condition with the particular resultant value that is "resultId".If satisfies the condition then we are record in the object using FirstOrDefault(); method.


Here the use FirstOrDefault(); method is, it gives first one if there are multiple record with multiplt values.if the value doesn't exists then it gives the default value that is null value.In next Blog i will try to Explain singleorDefault() method