本文介绍了无法隐式转换类型'System.Collections.Generic.List的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public List< spectruminfo> GetSpectrumBandList(uint StartTime) { List< spectruminfo> splist = new List< spectruminfo>(); using(System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(DbApi.ConnectionString) ) { conn.Open(); var spectrumItems = DbApi.GetSpectrumBandStartValue(conn,StartTime); foreach(SpectrumItems中的SpectrumItem项目) { SpectrumInfo spinfo = new SpectrumInfo(); spinfo.StartTime = item.startTime; spinfo.BandStart = item.bandStart; spinfo.PathId = item.pathId; spinfo。 CellId = item.cellid; splist.Add(spinfo); } } 返回splist; } var spectrumBandList = GetSpectrumBandList((uint)StartTime); //matchInfo.Channels = new List < int > (); matchInfo.Channels = spectrumBandList; 无法隐式转换类型'System.Collections.Generic.List < ProView.Web。 Services.SpectrumInfo > 'to'System.Collections.Generic.List < int > ' 我得到错误(matchInfo.Channels = spectrumBandList;)。如何解决这个问题解决方案 matchInfo.Channels的类型是什么。 如果它的类型是 List< int> 那么你就不能为它指定spectrumBandList。 你必须定义matchInfo。频道为列表< spectruminfo> 。 认真? 它告诉你究竟是什么问题是。 matchInfo.Channels的类型为System.Collections.Generic.List< int>你给它分配了一个值System.Collections.Generic.List< ProView.Web.Services.SpectrumInfo> 你需要设置一个不同的列表。 这是因为函数 GetSpectrumBandList()返回一个包含 splist 的列表一个的列表SpectrumInfo ProView.Web.Services.SpectrumInfo 这不能分配给整数列表,这就是你得到这样的错误的原因。你可以通过这样使用来避免这种情况: matchInfo.Channels = new List< spectruminfo>(); < / spectruminfo > public List<spectruminfo> GetSpectrumBandList(uint StartTime) { List<spectruminfo> splist = new List<spectruminfo>(); using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(DbApi.ConnectionString)) { conn.Open(); var spectrumItems = DbApi.GetSpectrumBandStartValue(conn, StartTime); foreach (SpectrumItem item in spectrumItems) { SpectrumInfo spinfo = new SpectrumInfo(); spinfo.StartTime = item.startTime; spinfo.BandStart = item.bandStart; spinfo.PathId = item.pathId; spinfo.CellId = item.cellid; splist.Add(spinfo); } } return splist; }var spectrumBandList = GetSpectrumBandList((uint)StartTime);//matchInfo.Channels = new List<int>();matchInfo.Channels = spectrumBandList;Cannot implicitly convert type 'System.Collections.Generic.List<ProView.Web.Services.SpectrumInfo>' to 'System.Collections.Generic.List<int>'I got error for (matchInfo.Channels = spectrumBandList;).how to solve this 解决方案 What is the type of matchInfo.Channels.If its type isList<int> then you cannot assign spectrumBandList to it.You have to define matchInfo.Channels as List<spectruminfo>.Seriously?It tells you exactly what the problem is.matchInfo.Channels is of type System.Collections.Generic.List<int> and you're assigning it a value of System.Collections.Generic.List<ProView.Web.Services.SpectrumInfo>You need to set a different list.this is because the function GetSpectrumBandList() returns a list splist that contains a list of SpectrumInfoProView.Web.Services.SpectrumInfo which cannot be assign to a list of integers that's why you are getting such error. you can avoid this by using like this :matchInfo.Channels = new List<spectruminfo>();</spectruminfo> 这篇关于无法隐式转换类型'System.Collections.Generic.List的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 08:28