c# 的问题。关于list<t>
publicstaticList<Data>Getlist(){List<Data>d=newList<Data>();Datar=null;stringsql="sel...
public static List<Data> Getlist()
{
List<Data> d = new List<Data>();
Data r = null;
string sql = "select * from Products ORDER BY prosortorder asc";
foreach (DataRow dr in Query.Getdataset(sql).Tables[0].Rows)
{
d.Add(r = new Data()
{
id = Convert.ToInt32(dr["id"]),
ProSortOrder = Convert.ToInt32(dr["ProSortOrder"]),
ProLookCount = Convert.ToInt32(dr["ProLookCount"]),
ProClassId = Convert.ToInt32(dr["ProClassId"]),
ProName = dr["ProName"].ToString(),
ProContent = dr["ProContent"].ToString(),
ProKey = dr["ProKey"].ToString(),
ProDescp = dr["ProDescp"].ToString(),
ProBigPic = dr["ProBigPic"].ToString(),
ProSmallPic = dr["ProSmallPic"].ToString(),
ProAttName = dr["ProAttName"].ToString(),
ProAttValue = dr["ProAttValue"].ToString()
}
);
}
return d;
}
我在3.5下这段代码没错。到2.0下就显示出错。是不是3.5 和2.0之间list<t>的语法不一样。如果是我该怎么写啊 展开
{
List<Data> d = new List<Data>();
Data r = null;
string sql = "select * from Products ORDER BY prosortorder asc";
foreach (DataRow dr in Query.Getdataset(sql).Tables[0].Rows)
{
d.Add(r = new Data()
{
id = Convert.ToInt32(dr["id"]),
ProSortOrder = Convert.ToInt32(dr["ProSortOrder"]),
ProLookCount = Convert.ToInt32(dr["ProLookCount"]),
ProClassId = Convert.ToInt32(dr["ProClassId"]),
ProName = dr["ProName"].ToString(),
ProContent = dr["ProContent"].ToString(),
ProKey = dr["ProKey"].ToString(),
ProDescp = dr["ProDescp"].ToString(),
ProBigPic = dr["ProBigPic"].ToString(),
ProSmallPic = dr["ProSmallPic"].ToString(),
ProAttName = dr["ProAttName"].ToString(),
ProAttValue = dr["ProAttValue"].ToString()
}
);
}
return d;
}
我在3.5下这段代码没错。到2.0下就显示出错。是不是3.5 和2.0之间list<t>的语法不一样。如果是我该怎么写啊 展开
5个回答
展开全部
for (int i=0;i< Query.Getdataset(sql).Tables[0].Rows.count;i++)
{ DataRow dr =Query.Getdataset(sql).Tables[0].Rows[i];
r = new Data();
r.id = Convert.ToInt32(dr["id"]),
r.ProSortOrder = Convert.ToInt32(dr["ProSortOrder"]),
r.ProLookCount = Convert.ToInt32(dr["ProLookCount"]),
r. ProClassId = Convert.ToInt32(dr["ProClassId"]),
r. ProName = dr["ProName"].ToString(),
r.ProContent = dr["ProContent"].ToString(),
r.ProKey = dr["ProKey"].ToString(),
r.ProDescp = dr["ProDescp"].ToString(),
r.ProBigPic = dr["ProBigPic"].ToString(),
r. ProSmallPic = dr["ProSmallPic"].ToString(),
r. ProAttName = dr["ProAttName"].ToString(),
r.ProAttValue = dr["ProAttValue"].ToString()
dr.Add(r );
}
如此吧
{ DataRow dr =Query.Getdataset(sql).Tables[0].Rows[i];
r = new Data();
r.id = Convert.ToInt32(dr["id"]),
r.ProSortOrder = Convert.ToInt32(dr["ProSortOrder"]),
r.ProLookCount = Convert.ToInt32(dr["ProLookCount"]),
r. ProClassId = Convert.ToInt32(dr["ProClassId"]),
r. ProName = dr["ProName"].ToString(),
r.ProContent = dr["ProContent"].ToString(),
r.ProKey = dr["ProKey"].ToString(),
r.ProDescp = dr["ProDescp"].ToString(),
r.ProBigPic = dr["ProBigPic"].ToString(),
r. ProSmallPic = dr["ProSmallPic"].ToString(),
r. ProAttName = dr["ProAttName"].ToString(),
r.ProAttValue = dr["ProAttValue"].ToString()
dr.Add(r );
}
如此吧
展开全部
在哪一行出错的,个人估计是 d.Add(r = new Data()这种匿名方法引起的,它在2.0中与3.5中确实是不一样的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
应该是2.0不支持在初始化对象时直接赋值,这个应该是3.5才有的功能。这个和List<t>没关系。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
有可能是版本间的兼容性问题,推荐使用3.5版本!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询