LINQ to get list of data before some given value condition

Hello,

Anyone please can suggest if in Linq I can select records .I am trying  to get  some data as list.
So for example in the attached worksheet if i can get list of data before NULL value yellow cell  of ColumnA.  Please assume this as a DataTable with columns.

Just need an idea of a linq query and c sharp.

Thanks.
abhilash
Book1.xlsx
Abhilash Nagar.NET DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
Is there some sort of ID column on the tables as well?  And what order do you want to retrieve the rows, certainly not random?

If there is an ID then I might approach it this way.  First, do a query to find the first NULL value in that column, sorting by ID or whatever other sort order you want to use.

Then do a second query (might be able to merge into one LINQ, but might be easier to understand as two) that returns all rows with an ID less than the one where the first NULL was found, and .ToList() that result set.


»bp
Fernando SotoRetiredCommented:
Hi Abhilash Nagar;
 
The Following query will return a List of DataRow types.
var results1 = (from rec in dt.AsEnumerable().TakeWhile(d => d.Field<String>("ColumnA") != null )
                select rec).ToList();

Open in new window


This query will return a List of Anonymous type.
var results2 = (from rec in dt.AsEnumerable().TakeWhile(d => d.Field<String>("ColumnA") != null)
                select new
                {
                    ColumnA = rec.Field<String>("ColumnA"),
                    ColumnB = rec.Field<int>("ColumnB")
                }).ToList();

Open in new window

Abhilash Nagar.NET DeveloperAuthor Commented:
Thank you , I will try this one and will let you know the output.

Thanks again for your time.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Fernando SotoRetiredCommented:
OK, if you have a question just ask.
Abhilash Nagar.NET DeveloperAuthor Commented:
Thanks for the response, it really helpful. One quick question again can we get result by index range for example from rowindex 0 -115 ,can we get those records using LINQ.

Abhilash
Fernando SotoRetiredCommented:
Hi Abhilash;

The List object has a count method and seeming the collection of the records starts a the first til it reaches the null value your index would be 0 - (Count - 1).

Fernando
Abhilash Nagar.NET DeveloperAuthor Commented:
Thanks everyone yes got the solution.Thanks.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernando SotoRetiredCommented:
Not a problem Abhilash, glad to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.