How can I make this to shorter one using Linq?
int id = 0;
foreach (DataRow dr in tableClientTableAdapter1.GetData())
{
if (dr[0].ToString() == txtClientName.Text)
{
id = Convert.ToInt16(dr[1]);
break;
}
}
I tried using this
var a = tableClientTableAdapter1.GetData().Cast<DataRow>().Where(cName => cName[0].ToString() == txtClientName.Text);
MessageBox.Show(a[1].ToString());
But I got this error:
Error 1 Cannot apply indexing with [] to an expression of type ‘System.Data.EnumerableRowCollection’ C:\Users\vrynxzent@yahoo.com\Desktop[Final][GlobalTek] Monitoring System[GlobalTek] Monitoring System\xfrmProjectAwarding.cs 89 37 [GlobalTek] Monitoring System
Any help!!
var a =is a sequence of DataRow items that you are trying to treat as a single object. If you desire one result, use one ofon the query, with the difference being your expectation for the result. If more than one item can be present but you’re only interested in the first of them, go with
First(). If only one item should match and it’s an error if there are more, go withSingle(). If, in either scenario, it’s possible for there to be no matches, use the appropriate*OrDefault()version.