Named Parameters in ValueTuple.Create

余生颓废 提交于 2019-12-10 16:49:34

问题


I was playing arround with the Value Tuple in C#.

First some demo data:

  #region 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 List<Category> Categories { get; } = new List<Category>()
        {
            new Category(){Name="Beverages", ID=001},
            new Category(){ Name="Condiments", ID=002},
        };

        public List<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},
        };

    }
    #endregion

Then a method using the demo data:

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);
}

So far no problems.

But if I want a result sorted by Product name I can do as following :

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);
        }

And here I have my problem: when using ValueTuple.Create(...) can I name the parameters, so the names could be used in a OrderBy

I was hoping for something like this:

select ValueTuple.Create(CategoryId : category.ID, ProductName : prod.Name)

and then use the name in my orderBy:

OrderBy(e => e.ProductName)

回答1:


You can directly create a named tuple within your Select and explictly indicate the names:

(
    from category in data.Categories
    join prod in data.Products on category.ID equals prod.CategoryID
    select (CategoryId: category.Id, ProductName: prod.Name)
).OrderBy(e => e.ProductName);



回答2:


Maybe it is better to provide same style to all linq query. I mean use "orderby" before select.

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
                orderby prod.Name
                select ValueTuple.Create(category.ID, prod.Name);
        }


来源:https://stackoverflow.com/questions/55178757/named-parameters-in-valuetuple-create

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!