Using linq to get data from a datatable
-
I have a standard requirement where I need to get an ID value from a datatable based on a string value from a description field. To date this is done either by a stored proc or using a filtered dataview or even a datatable.select. I would like to get it using Linq. So far most examples seem to return a query as ienumerable of datarows so they can be bound to controls. This seems to be the default MS example. I want the int value to be returned in one statement. Is this possible without recasting the result to a datatable/row to get the value.
Never underestimate the power of human stupidity RAH
-
I have a standard requirement where I need to get an ID value from a datatable based on a string value from a description field. To date this is done either by a stored proc or using a filtered dataview or even a datatable.select. I would like to get it using Linq. So far most examples seem to return a query as ienumerable of datarows so they can be bound to controls. This seems to be the default MS example. I want the int value to be returned in one statement. Is this possible without recasting the result to a datatable/row to get the value.
Never underestimate the power of human stupidity RAH
-
I'm actually after his 2nd example except I want both the select and the subsequent test for an existing record in 1 query.
iID Convert.ToInt32(data) = (from o in SupplierType
where o.Description = sDescription
select o.ID);I will then deal with the integer iID which will be 0 or a record ID value.
Never underestimate the power of human stupidity RAH
-
I'm actually after his 2nd example except I want both the select and the subsequent test for an existing record in 1 query.
iID Convert.ToInt32(data) = (from o in SupplierType
where o.Description = sDescription
select o.ID);I will then deal with the integer iID which will be 0 or a record ID value.
Never underestimate the power of human stupidity RAH
You could you FirstOrDefault which will return null if no value is present (or you can specify a default value).
int[] numbers = { 1, 2, 3, 4, 5, 11 };
// throws an exception
int firstBigNumber = (from p in numbers where p > 10 select p).First();// returns 0
int firstBigNumber = (from p in numbers where p > 10 select p).FirstOrDefault();// returns 111
int firstBigNumber = (from p in numbers where p > 100 select p).DefaultIfEmpty(111).FirstOrDefault(); -
You could you FirstOrDefault which will return null if no value is present (or you can specify a default value).
int[] numbers = { 1, 2, 3, 4, 5, 11 };
// throws an exception
int firstBigNumber = (from p in numbers where p > 10 select p).First();// returns 0
int firstBigNumber = (from p in numbers where p > 10 select p).FirstOrDefault();// returns 111
int firstBigNumber = (from p in numbers where p > 100 select p).DefaultIfEmpty(111).FirstOrDefault();Thank you - I can massage the third example with no trouble at all - I really need to get a book on this stuff shortly :-O
Never underestimate the power of human stupidity RAH
-
Thank you - I can massage the third example with no trouble at all - I really need to get a book on this stuff shortly :-O
Never underestimate the power of human stupidity RAH
-
I have a standard requirement where I need to get an ID value from a datatable based on a string value from a description field. To date this is done either by a stored proc or using a filtered dataview or even a datatable.select. I would like to get it using Linq. So far most examples seem to return a query as ienumerable of datarows so they can be bound to controls. This seems to be the default MS example. I want the int value to be returned in one statement. Is this possible without recasting the result to a datatable/row to get the value.
Never underestimate the power of human stupidity RAH
With thanks to ABitSmart a solution was found.
string sDescription = "Source system";
int iTypeID = (from t in oTable.AsEnumerable()
where t.Field("Description").ToLower() == sDescription.ToLower()
select t.Field("TypeID")).FirstOrDefault();iTypeID has a value of 0 ot the matching ID
Never underestimate the power of human stupidity RAH