[英]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. 任何帮助将不胜感激。
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
。
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 })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.