首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

怎么查询多个字段,返回model数组?

2012-01-07 
如何查询多个字段,返回model数组??要求:1.返回model数组2.利用Lamdom表达式查询出表中的某些字段,不是所有

如何查询多个字段,返回model数组??
要求:1.返回model数组
  2.利用Lamdom表达式查询出表中的某些字段,不是所有字段,也不是一个字段

public DataBase.Model.Moni_AccountInfo[] GetModelList()
  {

  DataBase.Model.AccountInfoDataContext context = null;
  DataBase.Model.Moni_AccountInfo[] info = new DataBase.Model.Moni_AccountInfo[0];

  try
  {
  context = new DataBase.Model.AccountInfoDataContext();

  var r = (from n in context.Moni_AccountInfo select new { n.ID,n.InFutureZR,n.InFutureZYK});
  //这里怎么将r转换成model数组??
  info = r.ToList<DataBase.Model.Moni_AccountInfo>(); //出现错误
  }
  catch
  {

  }
  finally
  {
  if (context != null)
  {
  context.Dispose();
  }
  }
  return info;
  }

[解决办法]
.ToArray();
[解决办法]
这个问题我也碰到过
不知道怎么弄
坐等高手
[解决办法]
你的Moni_AccountInfo类里面只有n.ID,n.InFutureZR,n.InFutureZYK 这三个属性吗??
[解决办法]
DataBase.Model.Moni_AccountInfo[] info = new DataBase.Model.Moni_AccountInfo[0];
这句不要,删除掉!
然后:
var info = r.ToList<DataBase.Model.Moni_AccountInfo>();
[解决办法]

探讨
你的Moni_AccountInfo类里面只有n.ID,n.InFutureZR,n.InFutureZYK 这三个属性吗??

[解决办法]
把这三个属性封装成一个实体。。

再用5楼的方法····
[解决办法]
select new { n.ID,n.InFutureZR,n.InFutureZYK});
你这么用是一个匿名类,当然不能转换了

你需要把查询的字段封装一下,再tolist就可以了
例如
select new YourNewModel{
id = ..
}
[解决办法]
C# code
return (from n in context.Moni_AccountInfo  select new Moni_AccountInfo   {     ID=n.ID,     InFutureZR=n.InFutureZR,     InFutureZYK=n.InFutureZYK   }).ToArray();
[解决办法]
我通常都是把返回类型设置成 object[] ,然后任意得到的结果集都 .ToArray()搞定。

外面的数据绑定控件会自己识别的。

热点排行