简体   繁体   English

Lambda Expression的新手,如何从一种类型转换为另一种类型

[英]New to Lambda Expression how to convert from one type to another type

I'm trying to return a list of countries, and assigning the return values to CountryCode and CountryName, I keep getting these errors: 我试图返回国家/地区列表,并将返回值分配给CountryCode和CountryName,但不断收到以下错误消息:

Cannot implicitly convert type 'System.Collections.Generic.List to 'System.Linq.IQueryable'. 无法将类型“ System.Collections.Generic.List”隐式转换为“ System.Linq.IQueryable”。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

and

Operator '==' cannot be applied to operands of type 'string' and 'System.Collections.Generic.List 运算符'=='不能应用于类型为'string'和'System.Collections.Generic.List的操作数

Here is my code: 这是我的代码:

public class CountryViewModel
{
    public string CountryCode { get; set; }
    public string CountryName { get; set; }


    /*SELECT COUNTRIES */

    public static IQueryable<CountryViewModel> GetCountries()
    {
        DbEntities _context = new DbEntities();

        var featureCode = "PCLI";

        var countries = _context.country
                        .Where(c => c.Feature_Code == featureCode
                        .Select(n => new CountryViewModel
                        {
                            CountryCode = c.Country_Code,
                            CountryName = c.Name
                        }
                        ));

        return countries;

} }

I couldn't find a proper example to help me along with this, any help would be appreciated. 我找不到合适的例子来帮助我,任何帮助将不胜感激。

I think the closing ")" for the .Where clause should be after "featureCode", just before .Select. 我认为.Where子句的结尾“)”应该在“ featureCode”之后,在.Select之前。 The error message sounds like it's assuming the Select clause is to be part of the condition. 错误消息听起来像是假设Select子句将成为条件的一部分。

In you example the variable 'countries' you are returning is an IEnumerable not the IQueryable you have prototyped. 在您的示例中,您要返回的变量“国家”是一个IEnumerable而不是您已原型化的IQueryable。 I suppose you could return countries.AsQueryable(). 我想您可以返回country.AsQueryable()。 If you want to return a list then return countries.ToList(). 如果要返回列表,则返回countries.ToList()。

Quite a few things wrong with your code as far as I can see. 据我所知,您的代码有很多错误。 Also I'm assuming DbEntities implements IDisposable so you should probably wrap that in a using statement. 另外,我假设DbEntities实现IDisposable因此您应该将其包装在using语句中。

public class CountryViewModel
{
    public string CountryCode { get; set; }
    public string CountryName { get; set; }

    public static IQueryable<CountryViewModel> GetCountries()
    {
        using (DbEntities _context = new DbEntities())
        {
            var featureCode = "PCLI";

            var countries = _context.country
                .Where(c => c.Feature_Code == featureCode)
                .Select(c => new CountryViewModel
                {
                    CountryCode = c.Country_Code,
                    CountryName = c.Name
                }
            );

            return countries;
        }
    }
}

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

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