Fatih Tan
Fatih Tan

Reputation: 808

Sorting a list by different type of list property

I have two lists of two different types.

List types are ProductPackageRelation and Package. I want to sort the list of Package by using list of ProductPackageRelation with Sequence property.

Will be used for the sorting criteria:

public class ProductPackageRelation : BaseDTO
{
    public int PackageID { get; set; }
    public int ProductID { get; set; }
    public int Sequence { get; set; }  // This is the sorting property
}

Will sort:

public class Package : BasePackage
{
    public int GuideID { get; set; }
}

Here are my lists:

var productPackageRelationList = _iPackageService.GetRelations();
var packageList = _iPackageService.GetPackages();


// Example ProductPackageRelationList
ID  ProductID   PackageID   Sequence
1      5            1         3
2      5            2         2
3      5            3         5
4      5            4         4
5      5            5         1



// Example PackageList
ID  GuideID
1     1
2     2
3     3
4     4
5     1

packageList variable will be sorted by productPackageRelationList variable's Sequence property.

How can it be sorted?

Upvotes: 0

Views: 73

Answers (2)

user7321040
user7321040

Reputation:

Join the lists and order it:

var q = (from p in packageList
        join r in productPackageRelationList on p.Id equals r.PackageID     
        select p).OrderBy( i => i.Sequence);

or OrderByDescending() if you want it upside down

Upvotes: 1

Magnus
Magnus

Reputation: 46909

Join the lists and than order by Sequence:

var q = from p in packageList
        join r in productPackageRelationList on p.Id equals r.PackageID
        orderby r.Sequence
        select p;

Upvotes: 4

Related Questions