How can I extract a list of Tuple from a specific table with Entity Framework / LINQ?

前端 未结 2 1903
闹比i
闹比i 2020-12-19 01:47

I need to extract a list of couple \'ID\'/\'Name\' from a large table in C# .NET with Entity Framework.

I try this request :

List

        
2条回答
  •  臣服心动
    2020-12-19 02:12

    You can do it with a middle-step by selecting an anonymous type:

    db.Resource.Select(x => new { x.Resource_ID, x.Name }).AsEnumerable().Select(x => Tuple.Create(x.Resource_ID, x.Name)).ToList();
    

    Creating a tuple is not a supported operation in Linq To Entities, so you have to select an anonymous type, which would be an equivalent to:

    SELECT [Resource].[Resource_ID], [Resource].[Name]
    

    then move to LINQ to Objects by AsEnumerable and get your tuple.

提交回复
热议问题