简体   繁体   English

LINQ错误:无法识别方法

[英]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. LINQ to Entities无法识别方法'System.String ToString()',并且该方法无法转换为商店表达式。

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 ? City是什么类型? If it's already a string, just drop the .Distinct().ToString() calls. 如果已经是字符串,则只需删除.Distinct().ToString()调用即可。 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. 更新 :根据您的评论,您应该删除Distint()和ToString()调用。 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);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM