简体   繁体   English

Web API无法隐式转换类型'System.Collections.Generic.List <

[英]Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

I am creating a Web API in I know there are lots of questions asked about it but I have tried their solution but didn't work for me. 我正在创建Web API ,因为我知道有很多问题要问,但是我尝试了他们的解决方案,但对我没有用。 I am declaring a list which I will be using at several points in my list. 我要声明一个列表,该列表将在列表中的多个位置使用。

public HttpResponseMessage GetDetails(string msn, DateTime dt)
{
   try
    {
          var mainDetails= new List<string>();

          int mainCount = giveMainCount(msn, dt);

           if(mainCount==0)
           {
              // here I want to set the list empty like mainDetails = null or "" like this
           }

            int mainInterval = mainCount / 500;

            mainDetails = kesc.tj_xhqd
                         .AsNoTracking()
                         .Where(m => (m.zdjh == msn) && (m.sjsj >= dt))
                         .AsEnumerable()
                         .Select((x, i) => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd, i = i })
                         .Where(x => x.i % mainInterval == 0)
                         .ToList(); // here I am getting error 
       return Request.CreateResponse(HttpStatusCode.OK, new { details = mainDetails });

    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }


    }
}

The error is 错误是

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string MSN, System.DateTime? 无法隐式转换类型'System.Collections.Generic.List <<匿名类型:字符串MSN,System.DateTime? PingDateTime, string PingValue, int i>>' to 'System.Collections.Generic.List<string>' PingDateTime,字符串PingValue,int >> >>到'System.Collections.Generic.List <string>'

How can I get rid of this error ? 如何摆脱这个错误?

Any help would be highly appreciated. 任何帮助将不胜感激。

Option 1 选项1

You could go with not declaring mainDetails from the beginning, doing something like: 您可以从一开始就不声明mainDetails ,例如:

public HttpResponseMessage GetDetails(string msn, DateTime dt)
{
    try
    {
        int mainCount = giveMainCount(msn, dt);

        if (mainCount == 0)
        {
            return Request.CreateResponse(HttpStatusCode.OK, new { details = null });
        }

        int mainInterval = mainCount / 500;

        var mainDetails = kesc.tj_xhqd
                        .AsNoTracking()
                        .Where(m => (m.zdjh == msn) && (m.sjsj >= dt))
                        .AsEnumerable()
                        .Select((x, i) => new { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd, i = i })
                        .Where(x => x.i % mainInterval == 0)
                        .ToList();

        return Request.CreateResponse(HttpStatusCode.OK, new { details = mainDetails });

    }
    catch (Exception ex)
    {
        return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
    }
}

Doing it this way, mainDetails will be what you want, a List of that anonymous type with aliases. 这样, mainDetails将成为您想要的,带有别名的匿名类型的List


Option 2 选项2

As an alternative option, you could create a class for the aliases (use what you want instead of AliasClassName ): 作为替代选择,您可以为别名创建一个类(使用所需的名称代替AliasClassName ):

public class AliasClassName
{
    public string MSN { get; set; }
    public Nullable<System.DateTime> PingDateTime { get; set; }
    public string PingValue { get; set; }
    public int i { get; set; }
}

Then declare mainDetails as a List of that: 然后将mainDetails声明mainDetails List

var mainDetails = new List<AliasClassName>();

And then do: 然后执行:

.Select((x, i) => new AliasClassName { MSN = x.zdjh, PingDateTime = x.sjsj, PingValue = x.xhqd, i = i })

I have found a solution and it works for me 我找到了解决方案,对我有用

Changed 已变更

var mainDetails= new List<tj_xhqd>();

To

IEnumerable mainDetails= new List<tj_xhqd>();

By doing this I got rid of my error 通过这样做,我摆脱了我的错误

For further details, one can check it here 有关更多详细信息,可以在此处进行检查

暂无
暂无

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

相关问题 无法隐式转换类型&#39;System.Collections.Generic.List &lt; <anonymous type> &gt;”到“ System.Collections.Generic.List <string> &#39; - Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type>>' to 'System.Collections.Generic.List<string>' 不能隐式转换类型 &#39;System.Collections.Generic.List&lt; - Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type 无法隐式转换类型 System.Collections.Generic.List&lt;<anonymous type:int,string,bool> &gt;</anonymous> - Cannot implicitly convert type System.Collections.Generic.List<<anonymous type:int,string,bool>> 无法隐式转换类型'System.Collections.Generic.List&lt;<anonymous type: string access> &gt;' 到 'System.Collections.Generic.IEnumerable<string> '</string></anonymous> - Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string Access>>' to 'System.Collections.Generic.IEnumerable<string>' 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T> 如何修复“无法将类型System.Collections.Generic.List &lt;&gt;隐式转换为System.Collections.Generic.List &lt;&gt;” - How to fix 'Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>' 无法隐式转换类型'System.Collections.Generic.List<x> ' 到 'System.Collections.Generic.List<y> '</y></x> - Cannot implicitly convert type 'System.Collections.Generic.List<x>' to 'System.Collections.Generic.List<y>' 无法隐式转换类型&#39;System.Collections.Generic.List错误 - Cannot implicitly convert type 'System.Collections.Generic.List error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM