I have an asp.net application, and now I am using datasets for data manipulation. I recently started to convert this dataset to a List collection. But, in some places it doesn't work. One is that in my old version I am using datarow[] drow = dataset.datatable.select(searchcriteria). But in the List collection there is no method available for finding particular values. Is there any way for me to select some values according with my search criteria? I want to know if this is possible. Please help me.

6

Best Answer


Well, to start with List<T> does have the FindAll and ConvertAll methods - but the more idiomatic, modern approach is to use LINQ:

// Find all the people older than 30var query1 = list.Where(person => person.Age > 30);// Find each person's namevar query2 = list.Select(person => person.Name);

You'll need a using directive in your file to make this work:

using System.Linq;

Note that these don't use strings to express predicates and projects - they use delegates, usually created from lambda expressions as above.

If lambda expressions and LINQ are new to you, I would suggest you get a book covering LINQ first, such as LINQ in Action, Pro LINQ, C# 4 in a Nutshell or my own C# in Depth. You certainly can learn LINQ just from web tutorials, but I think it's such an important technology, it's worth taking the time to learn it thoroughly.

When working with lists in C#, you may often need to select specific items from the list based on certain conditions. In this article, we will explore various ways to accomplish this task using C#.

One common approach is to use the Where method provided by LINQ. This allows you to specify a predicate function that defines the criteria for selecting items from the list. The Where method returns a new IEnumerable that contains only the items that meet the specified criteria.

Another option is to use the FindAll method, which is available for lists. This method also takes a predicate function as an argument and returns a new List containing all the items that satisfy the condition.

If you only need to select a single item from the list, you can use the FirstOrDefault or SingleOrDefault methods. These methods return the first item that matches the condition or a default value if no item is found.

In conclusion, there are several ways to select from a list in C#. The method you choose depends on your specific requirements and the complexity of the condition you need to apply.

you can also try

var query = from p in listwhere p.Age > 18select p;

Try this:

using System.Data.Linq;var result = from i in listwhere i.age > 45select i;

Using lambda expression please use this Statement:

var result = list.where(i => i.age > 45);

Generic List<T> have the Where<T>(Func<T, Boolean>) extension method that can be used to filter data.

In your case with a row array:

var rows = rowsArray.Where(row => row["LastName"].ToString().StartsWith("a"));

If you are using DataRowCollection, you need to cast it first.

var rows = dataTableRows.Cast<DataRow>().Where(row => row["LastName"].ToString().StartsWith("a"));

I have used a script but to make a join, maybe I can help you

string Email = String.Join(", ", Emails.Where(i => i.Email != "").Select(i => i.Email).Distinct());