Link to home
Create AccountLog in
Avatar of clooak
clooak

asked on

Linq or SQL - Check data from datatable if column data contains a letter "E"

Hey

I am developing a .NET web-app and I am importing a huge amount of data from an excel sheet, to a datatable.

Before I do any changes to the data fetched, I want to make sure that a certain field in the excel, does not contain character "E". If so, stop the import process.

Any idea what this linq query should look like? I have tried numerous of ideas, with no result.

I would rather use an SQL query, but couldnt find a way to query the dataset by using it.
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi clooak;

Something like the following code snippet should work. Make sure to assign the correct table index.

var results = (from row in DataSetInstance.Table[0].AsEnumerable()
               where row.Field<string>("ColumnNameHere").Contains("E") 
               select row).Count();
               
if( results == 0 )
{
    // Do the processing
}
else
{
    // E was found in the column do NOT do processing.
}

Open in new window

Avatar of clooak
clooak

ASKER

Splendid perfect! Theres only one problem left which I can't really figure out..

The data i am comparing is looking similar to 124245E2414 notice the E in the middle, I can't  get it to convert the entire value to string, so that I can compare it.

Any idea? I have tried setting soemthing like .ToString() so it looks something like
row.Field<string>("Order_num").ToString().Contains("E")
Avatar of clooak

ASKER

If I changed the
row.Field<double>("Order_num")
to row.Field<string>("Order_num").ToString()

Then It works with the E values, but the rest that doesnt have E is still considered as Double will yield error when trying to read them.

So I gotta figure out a way to force the Double/String I am reading to string. Somehow.
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer