日期:2014-05-18  浏览次数:21303 次

foreach语句 指定的转换无效。
public List<WindowsFormsApplication1.model.Good> searchByCondition(string[] conditions)
  {
  List<WindowsFormsApplication1.model.Good> list = new List<WindowsFormsApplication1.model.Good>();

  GoodsManaDataContext db = new GoodsManaDataContext(connString);
  var query =
  from g in db.Goods
  select g;

  foreach (var g in query)// in 出错处
  {
  WindowsFormsApplication1.model.Good good = new WindowsFormsApplication1.model.Good();
  good.Id = g.Gid;
  good.Name = g.Gname;
  good.Price = g.Gprice;
  good.Description = g.Gdescrip;
  good.Date = g.Gdate;
   
  list.Add(good);
  }
  return list;
  }



------解决方案--------------------
蛋疼的写法。命名空间没导入的原因。

直接如下:
C# code

return db.Goods.Select(g => new WindowsFormsApplication1.model.Good
{
  good.Id = g.Gid,
  good.Name = g.Gname,
  good.Price = g.Gprice,
  good.Description = g.Gdescrip,
  good.Date = g.Gdate,
}).ToList();