0

I have a DataTable named dt. It has two Columns named atype_code and module. Now i need to query a module based on specific atype_code.

Here is the code i wrote but not working.

DataTable dt = GetATypeCodeList();
var accType = (from myAccType in dt.AsEnumerable()
           where myAccType.Field<string>("atype_code") == aTypeCode.Trim()
           select myAccType.Field<string>("module"));

acctype is a System.Data.EnumerableRowCollection<System.String>.

1
  • accType is showing {System.Data.EnumerableRowCollection<object>}how can i get the string out of it??? Thanks Commented Jun 27, 2012 at 10:21

1 Answer 1

1

Since you've said that you

need to query a module based on specific atype_code

, i assume that you want only one module with the given atype_code.

Then you should either use Single/SingleOrDefault or First/FirstOrDefault.

String firstModule = dt.AsEnumerable()
                       .Where(r => r.Field<string>("atype_code") == aTypeCode.Trim())
                       .Select(r => r.Field<string>("module"))
                       .FirstOrDefault();
// or
String singleModule = dt.AsEnumerable()
                       .Where(r => r.Field<string>("atype_code") == aTypeCode.Trim())
                       .Select(r => r.Field<string>("module"))
                       .SingleOrDefault();

Enumerable.Single throws an exception if there is more than one matching element. That can be useful to validate the data.

Edit:

Here's the query-syntax:

IEnumerable<String> modules =  
    from myAccType in dt.AsEnumerable()
    where myAccType.Field<string>("atype_code") == aTypeCode.Trim()
    select myAccType.Field<string>("module");
String module = modules.FirstOrDefault(); // returns null if no matching modules were found
Sign up to request clarification or add additional context in comments.

1 Comment

Showing errors in Cannot convert type 'System.Data.DataRow' to 'String'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.