Convert anonymous type to new C# 7 tuple type

前端 未结 5 1997
情书的邮戳
情书的邮戳 2021-01-07 16:19

The new version of C# is there, with the useful new feature Tuple Types:

public IQueryable Query();

public (int id, string name) GetSomeIn         


        
5条回答
  •  渐次进展
    2021-01-07 17:01

    The short answer is no, in the current form of C#7 there is no in-framework way to accomplish your goals verbatim, since you want to accomplish:

    • Linq-to-entities
    • Mapping to a subset of columns
    • Avoiding property by property mapping from a custom or anonymous type to a C#7 tuple by mapping directly to a C#7 tuple.

    Because Query exposes an IQueryable, any sort of projection must be made to an expression tree .Select(x => new {}).

    There is an open roslyn issue for adding this support, but it doesn't exist yet.

    As a result, until this support is added, you can either manually map from an anonymous type to a tuple, or return the entire record and map the result to a tuple directly to avoid two mappings, but this is obviously inefficient.


    While this restriction is currently baked into Linq-to-Entities due to a lack of support and the inability to use parametered constructors in a .Select() projection, both Linq-to-NHibernate and Linq-to-Sql allow for a hack in the form of creating a new System.Tuple in the .Select() projection, and then returning a ValueTuple with the .ToValueTuple() extension method:

    public IQueryable Query();
    
    public (int id, string name) GetSomeInfo() {
        var obj = Query()
            .Select(o => new System.Tuple(o.Id, o.Name))
            .First();
    
        return obj.ToValueTuple();
    }
    

    Since System.Tuple can be mapped to an expression, you can return a subset of data from your table and allow the framework to handle mapping to your C#7 tuple. You can then deconstruct the arguments with any naming convention you choose:

    (int id, string customName) info = GetSomeInfo();
    Console.Write(info.customName);
    

提交回复
热议问题