1) First
in linq, returns a single record. you will want to use Where
to return a list.
2) Where
in linq will return a collection of type IEnumerable<T>
. You will either need to call .ToList()
on it afterwards, or use var
as the type.
3) You shouldn't need to create a new data context to run this against, as long as you have the original table in a queryable format.
final code, by example
private void button3_Click(object sender, EventArgs e)
{
var myLambda = MyDataTable.AsEnumerable().Where(lambda => lambda.Field<string>("First_Name").StartsWith(TxtFirstName.Text));
//var should be of the type IEnumerable<DataRow>
//from here, we can use this var as the DataSource for another display
resultsBox.DataSource = myLambda;
//assuming resultsBox can interpret a datarow correctly.
//You may need to select First_Name only, or some other data, out of the returned values.
}
Alternatively, using the data context
private void button3_Click(object sender, EventArgs e)
{
MyLinqDataContext MyData = new MyLinqDataContext();
var myLambda = MyData.MyLists.Where(lambda => lambda.First_Name.StartsWith(TxtFirstName.Text));
resultsBox.DataSource = myLambda;
}