linq中的数组如何实体? [英] How get array in linq to entity?

查看:120
本文介绍了linq中的数组如何实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在$ db code code
{
国家= country.Title,
City = country.Cities
.Select(m => m.Title).ToArray()
};

城市 - 数组字符串。



需要获取数组Cities.Title



此代码:

  foreach var item in sites)

获取此错误:


LINQ to Entities的表达式不能识别方法
System.String [] ToArray [String]
(System.Collections.Generic.IEnumerable` 1 [System .String]),所以它
不能被转换成表达式存储。



解决方案

您应该可以投射到匿名类型,然后使用 ToArray() AsEnumerable():

  var sites = 
(from country in db.Countries
选择新
{
国家= country.Title,
城市= country.Cities.Select(m => m.Title)
})
.AsEnumerable()
。选择(country => new SitiesViewByUser()
{
Country = country.Title,
City = country.Cities.ToArray()
};

问题是 ToArray()根本没有为Linq to Entities定义 IQueryable provider(SQL中的等效调用是什么?)。因此,您必须抓住您的结果,切换到对象,然后然后,您可以根据需要实现它们。


var sites = 
    from country in db.Countries
    select new SitiesViewByUser()                
    {         
        Country = country.Title, 
        City = country.Cities
            .Select(m => m.Title).ToArray()                                   
    };

City - array string.

I need get array Cities.Title

this code:

foreach(var item in sites)

get this error:

Expression of LINQ to Entities does not recognize the method "System.String [] ToArray [String] (System.Collections.Generic.IEnumerable` 1 [System.String]) ", so it can not be converted into an expression store.

解决方案

You should be able to project to an anonymous type, then use ToArray() once you are back in Linq to objects land by using AsEnumerable():

var sites = 
    (from country in db.Countries
    select new 
    { 
         Country = country.Title,
         Cities = country.Cities.Select(m => m.Title)
    })
    .AsEnumerable()
    .Select(country => new SitiesViewByUser()
    {         
        Country = country.Title, 
        City = country.Cities.ToArray()
    };

The problem is that ToArray() is simply not defined for the Linq to Entities IQueryable provider (what would be the equivalent call in SQL?). Hence you have to grab your results, switch to Linq to Objects and then you can materialize them as needed.

这篇关于linq中的数组如何实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆