[英]Can someone advise me what wrong with my Type or linq query
我希望有人可以提供帮助,我的错误是
无法将类型system.collections.generic.List隐式转换为xxxlistlitems
我有这个
public IDListItems getIDList()
{
IDListItems items = new IDListItem();
try
{
var x = (from c in db.ap_GetIDListItems()
select new IDListItems { CId = c.CID, Id = c.ID }).ToList();
items = x;
}
catch (Exception ex)
{
throw ex;
}
return items;
}
然后我有一个这样的课
namespace SaSs
{
public class IDListItems
{
public int Id {get; set;}
public string CId { get; set; }
}
}
我认为这是我的返回类型的问题,但不确定如何返回该类型的列表
嗯...因为项目不是清单?
List<IDListItems> items = new List<IDListItem>();
应该做的把戏..
其他人建议更改变量的类型-我建议完全删除变量以及无意义的catch块。 当然,您还需要更改返回类型:
public List<IDListItems> getIDList()
{
return (from c in db.ap_GetIDListItems()
select new IDListItems { CId = c.CID, Id = c.ID }).ToList();
}
或者没有一些毫无意义的查询表达式:
public List<IDListItems> getIDList()
{
return db.ap_GetIDListItems()
.Select(c => new IDListItems { CId = c.CID, Id = c.ID })
.ToList();
}
items是一个IDListItems
。 您不能将IDListItems
分配给List<IDListItems>
。
尝试:
public List<IDListItems> getIDList()
{
List<IDListItems> items = new List<IDListItems>();
try
{
var x = (from c in db.ap_GetIDListItems()
select new IDListItems { CId = c.CID, Id = c.ID }).ToList();
items = x;
}
catch (Exception ex)
{
throw ex;
}
return items;
}
您需要将其设置为这样的列表
List<IDListItems> items = new List<IDListItem>();
这样的事情应该工作
public List<IDListItems> GetIDList()
{
List<IDListItems> items = new List<IDListItems>();
try
{
var x = (from c in db.ap_GetIDListItems()
select new IDListItems { CId = c.CID, Id = c.ID }).ToList();
items = x;
}
catch (Exception ex)
{
throw ex;
}
return items;
}
如果调用GetIDList()
,则也需要更改它。 您还需要为此添加适当的catch语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.