简体   繁体   中英

Named Parameters in ValueTuple.Create (2)

In an other SO Question i asked about named parameters in ValueTuple.Create. But since my example code wasn't true to my problem I didn't get the answer I wanted. So here we go again:

First demo data:

public class Product
    {
        public string Name { get; set; }
        public int CategoryID { get; set; }
    }

    public class Category
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }

    public class Data
    {
        public IQueryable<Category> Categories { get; } = new List<Category>()
        {
            new Category(){Name="Beverages", ID=001},
            new Category(){ Name="Condiments", ID=002},
        }.AsQueryable();

        public IQueryable<Product> Products { get; } = new List<Product>()
        {
            new Product{Name="Cola",  CategoryID=001},
            new Product{Name="Tea",  CategoryID=001},
            new Product{Name="Mustard", CategoryID=002},
            new Product{Name="Pickles", CategoryID=002},
        }.AsQueryable();

    }

NOTE IQueryable

Then to my problem:

    public static IEnumerable<(int CategoryId, string ProductName)> GetList()
    {
        var data = new Data();
        return
            (from category in data.Categories
                join prod in data.Products on category.ID equals prod.CategoryID
                select ValueTuple.Create(category.ID, prod.Name)).OrderBy(e => e.Item2);
    }

But if I want a result sorted by Product name I have to use OrderBy(e => e.Item2) . Can I somehow get rid of the Item1 and Item2 names in my Order by

Since I have an IQueryable interface I have to use ValueTuple.Create

I've found a workarround,: I could make a type case:

    public static IEnumerable<(int CategoryId, string ProductName)> GetList()
    {
        var data = new Data();
        return
            (from category in data.Categories
                join prod in data.Products on category.ID equals prod.CategoryID
                select ((int CategoryId, string ProductName))ValueTuple.Create(category.ID, prod.Name)).OrderBy(e => e.ProductName);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM