How can I use LINQ to sort and filter items in a List <ReturnItem> collection?

It's hard to explain.

We have a DataTable that contains a user-configurable set of columns that are unknown at compile time. Each column in a DataTable is of type String. We need to convert this DataTable into a strongly typed collection of ReturnItem objects so that we can sort and filter using LINQ for use in our application.

We have made some progress as follows:

  • We started with a basic DataTable.
  • Then we process the DataTable, creating a new ReturnItem object for each row
  • The "ReturnItem" object has only two properties: ID (row) and columns (list (object)). The property collection contains one record for each column representing one DataRow.
  • Each property is created strictly typed (int, string, datetime, etc.). For example, it will add a new DateTime object to the ReturnItem column list containing the value of the Created Datatable Column.
  • The result is a List (ReturnItem), which we would like to be able to sort and filter using LINQ based on the value in one of the properties, for example, sort by Date Created.

We use the LINQ dynamic query library, which still gets to us, but it doesn't seem like a way forward, because we use it in the List Collection.

Basically, my question boils down to the following: How can I use LINQ to sort and filter items in a List (ReturnItem) collection based on the values ​​in the List (object) property, which is part of the ReturnItem class?

+4
source share
2 answers

I'm not sure I understand what the problem is. Assuming you know the index of the column you want to sort, and these are pretty trivial types, can't you just do something like ...

void Main() { List<ReturnItem> items = new List<ReturnItem>(); items.Add(new ReturnItem() { ID = 1, Columns = new List<object>() { DateTime.Now, "donkey" } }); items.Add(new ReturnItem() { ID = 2, Columns = new List<object>() { DateTime.Now.AddHours(3), "baboon" } }); items.Add(new ReturnItem() { ID = 3, Columns = new List<object>() { DateTime.Now.AddHours(2), "antelope" } }); IEnumerable<ReturnItem> sortedByDate = items.OrderBy(x => x.Columns[0]); IEnumerable<ReturnItem> sortedByAnimal = items.OrderBy(x => x.Columns[1]); IEnumerable<ReturnItem> filteredByBaboon = items.Where(x => x.Columns[1] == "baboon"); } public class ReturnItem { public int ID; public List<object> Columns; } 
+3
source

Check this.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { List<ReturnItem> list = new List<ReturnItem>(); list.Add(new ReturnItem(1, "mouse", DateTime.Now)); list.Add(new ReturnItem(2, "mickey",DateTime.Now)); list = list.OrderBy(i => i._column._name).ToList(); list.ForEach(i => Console.WriteLine(i._column._name)); Console.Read(); } } class ReturnItem { public int _id; public Columns _column; public ReturnItem(int id, string name, DateTime date) { _id = id; _column = new Columns(name, date); } } class Columns { public string _name; public DateTime _date; public Columns(string name, DateTime date) { _name = name; _date = date; } } } 
0
source

Source: https://habr.com/ru/post/1308981/


All Articles