我怎样才能提取元组的列表,从一个特定的表实体框架/ LINQ?实体、框架、列表、LINQ

2023-09-04 01:00:25 作者:夕亦残潋若丶瑾残醉瞳风﹌

我需要从一个大表在C#.NET与实体框架提取夫妻双方的身份证/名称列表。

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

我尝试这个请求:

List<Tuple<int, string>> list = (from res in db.Resource 
                                 select new Tuple<int, string>(res.Resource_ID, res.Name)).ToList();

但不幸的是,我得到这个错误:

But unfortunately I've got this error :

Only parameterless constructors and initializers are supported in LINQ to Entities.

我不undestand我怎么能提取的元组使用此框架的这个名单,我觉得有点与此错误丢失。 你能帮助我了解和解决我的问题?

I don't undestand how can I extract this list of tuple with this framework and I feel a bit lost with this error. Can you help me to understand and resolve my problem ?

最好的问候,

亚历

推荐答案

您可以用一个中间步骤,选择一个匿名类型做到这一点:

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

创建一个元组是不是在SQL支持的操作,所以你必须选择一个匿名类型,这将是一个等价于:

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

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

然后通过 AsEnumerable 移动到LINQ到对象,让你的元组。

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