开发者

How can I use LINQ, to Sort and Filter items in a List<ReturnItem> collection?

This is tricky to explain.

We have a DataTable that contains a user configurable selection of columns, which are not known at compile time. Every column in the DataTable is of type String. We need to convert this DataTable into a strongly typed Collection of "ReturnItem" objects so that we can then sort and filter using LINQ for use in our application.

We have made some progress as follows:

  1. We started with the basic DataTable.
  2. We then process the DataTable, creating a new "ReturnItem" object for each row
  3. This "ReturnItem" object has just two properties: ID ( string ) and Columns( List(object) ). The properties collection contains one entry for each column, representing a single DataRow.
  4. Each property is made Strongly Typed (int, string, datetime, etc). For example it would add a new "DateTime" object to the "ReturnItem" Columns List containing the value of the "Created" Datatable Column.
  5. The result is a List(Retu开发者_运维知识库rnItem) that we would then like to be able to Sort and Filter using LINQ based on the value in one of the properties, for example, sort on "Created" date.

We have been using the LINQ Dynamic Query Library, which gets us so far, but it doesn't look like the way forward because we are using it over a List Collection of objects.

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


I'm not sure I'm understanding what the problem is. Assuming you know the index of the column you want to sort on and they are fairly 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;
}


Test 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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜