LINQ to get  list of  data before some given value condition

Abhilash Nagar
Abhilash Nagar used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
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 SotoRetired
Distinguished Expert 2017
Commented:
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 Developer

Author

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

Thanks again for your time.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Fernando SotoRetired
Distinguished Expert 2017

Commented:
OK, if you have a question just ask.
Abhilash Nagar.NET Developer

Author

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 SotoRetired
Distinguished Expert 2017

Commented:
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
.NET Developer
Commented:
Thanks everyone yes got the solution.Thanks.
Fernando SotoRetired
Distinguished Expert 2017

Commented:
Not a problem Abhilash, glad to help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial