开发者

LINQ Error: Method not recognized

I am using the following code which compiles without problems but I am getting this error when I call the method:

LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.

public IEnumerable<string> GetAllCitiesOfCountry(int id)
    {
        var ad = from a in entities.Addresses
                 where a.CountryID == id
                 select a.City.Distinct().ToString();
        var fa = from b in entities.FacilityAddresses
                 where b.CountryID == id
                 select b.City.Distinct().ToString();
        return ad.Concat(fa).Distinct();
    }

How can it be re开发者_开发百科-written in order to work?


Update - I think this is what you are looking for

public IEnumerable<string> GetAllCitiesOfCountry(int id)
    {
        var ad = from a in entities.Addresses
                 where a.CountryID == id
                 select a.City;
        var fa = from b in entities.FacilityAddresses
                 where b.CountryID == id
                 select b.City;
        return ad.Union(fa).Distinct();
    }


What type is City? If it's already a string, just drop the .Distinct().ToString() calls. If it's a complex type, select out the city name from the type.

Update: based on your comment, you should drop the Distint() and ToString() calls. The final Union over the collections of city names should give you unique city names.

public IEnumerable<string> GetAllCitiesOfCountry(int id) 
{ 
    var ad = from a in entities.Addresses 
             where a.CountryID == id 
             select a.City;
    var fa = from b in entities.FacilityAddresses 
             where b.CountryID == id 
             select b.City;
    return ad.Union(fa);
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜