开发者

How to sort collection by using linq method

I have a List<> (ListA) with many properties and one of them is a List<> (ListA_1). At this time, i sort (ListA) on strings properties by using linq method, like this :

var sortedCollection = from c in ListA
                       orderby selector(c), c.Caption
                       select c;

Caption is a string property of ListA.

I would like sort ListA with linq method, by his property (ListA_1) which contains many items object with strings properties. Is that possible by using linq method ?

thx for your help, Sebastien

Edit: Example:

public class ListA : List<ItemA>
{
}

public class ItemA
{
    private string mCaption;
    private ListA1  ListA1 = new ListA1();
}

public class ListA1 : List<ItemA1>
{
}

public class ItemA1
{
    private string mTitle;
    private string mRef;
    private string mType;
}

So, i have a ListA1 collection for a itemA of the ListA's collection. I would like to sort ListA by ListA1's property of itemA1.mType (from ListA)

Example :

var sortedCollection = from c in ListA
               开发者_Go百科        orderby selector(c), c.ListA1 (...ItemA1.mType....)
                       select c;


I personally prefer using the extension methods provided in System.Linq.

// collection is of type ListA
var sortedCollection = collection.OrderBy(x => x.mCaption);

Edit: I think I finally understand the question. As far as I know, there's no way to do this in a single line. The problem is, you need to define the resulting type of the query. The best solution I could think of would require a few lines of code. I would first create a List containing the two types, then sort by that.

var list = List<KeyValuePair<ItemA, ItemA1>>();
foreach (var item in collection)
    foreach (var item1 in item.ListA1)
        list.Add(new KeyValuePair<ItemA, ItemA1>(item, item1));
var sortedCollection = list.OrderBy(x => x.Value.mTitle);


I'm not entirely sure what it is you're asking. As I understood what you said, you want to sort ListA by Caption based on the ordering of in ListA_1 which is a list of captions. You could do this then:

var sorted = from c1 in ListA_1
             join c in ListA on c1 equals c.Caption
             select c;

[edit]
Part of the confusion I had with your question was that I did not know how you wanted to determine the order of the items in ListA based on the values of mType's in the list ListA_1. There is no natural ordering I can see. But now since I know that it is of type string, I can make some educated guesses as to what you mean.

If I can assume that all items in ListA1 have the same value for mType, then you just need to get the value from one of them and sort by that. You can get it by using FirstOrDefault().

var sorted = from c in ListA
             orderby selector(c), c.ListA1.FirstOrDefault().mType
             select c;

If it is possible to have different values of mType and you wanted to get the most common value, you would have to find the value that occurs the most.

var sorted = from c in ListA
             let mostCommonType = (from c1 in c.ListA1
                                   group c1.mType by c1.mType into g
                                   orderby g.Count() descending
                                   select g.Key).FirstOrDefault()
             orderby selector(c), mostCommonType
             select c;

I hope I captured your meaning in these possible solutions.


The constraint of the database do I need to have: A SQL table "Type" which contains a "Type" column and a "IdCatalog" column. A SQL table "Catalog" which contains a column "Id" and "Name" column.

For One catalog I Have Many Types wihch can be Added/updated/removed for this catalog.

These datas must be rendered as a HTml table with A column "IdCatalog" and all the columns "Types". I Build this render with object structure.

The render is tabular but data and behind code are not. And the difficulty is to sort the html table, with the object model that I built.

I think the best method would be to build a dataset from sql's datas to Work with a real table structure.


We can order by ListA1 if we implement IComparable.

Like this for example:

public class ListA1 : List<ItemA1>, IComparable{

    public int CompareTo(object obj)
    {
        if (obj is ListA1)
        {
            ListA1 l2 = (ListA1)obj;
            return this.ToString().CompareTo(l2.ToString());
        }
        else
            throw new ArgumentException("Object is not a ListA1.");
    }
    public String ToString()
    {
        return String.Join("", this.Select(item => item.mType));
    }

}

Then we can order by ListA1 in the same way we can order by a String.


If we don't want to implement IComparable we can do this:

var sortedCollection = from c in ListA 
                       orderby selector(c), GetValueWeWantToOrderBy(c.ListA1) 
                       select c;

And letting the function GetValueWeWantToOrderBy return a value we order by:

public static String GetValueWeWantToOrderBy(ListA1 la1)
{
    return String.Join("", la1.Select(item => item.mType));
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜