我需要从一个大表在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.
上一篇:在一个循环中的WPF应用程序,怎么没有整个应用程序冻结?应用程序、WPF
下一篇:什么时候需要手动处理的控制?我怎么知道,如果一个控制实现IDisposable?什么时候、我怎么、IDisposable