首页 新闻 赞助 找找看

linq to sql的返回值类型

0
悬赏园豆:20 [已关闭问题] 关闭于 2008-04-03 10:01
<P>我定义的实体类:</P> <P>[Table(Name="xc")]<BR>public class XC<BR>{<BR>&nbsp;&nbsp;&nbsp; [Column(IsPrimaryKey=true)]<BR>&nbsp;&nbsp;&nbsp; public int xcid { get; set; }</P> <P>&nbsp;&nbsp;&nbsp; [Column(Name = "xcname")]<BR>&nbsp;&nbsp;&nbsp; public string xcname { get; set; }</P> <P>&nbsp;&nbsp;&nbsp; [Column(Name = "categoryid")]<BR>&nbsp;&nbsp;&nbsp; public int categoryid { get; set; }</P> <P>&nbsp;&nbsp;&nbsp; [Column(Name = "xclogourl")]<BR>&nbsp;&nbsp;&nbsp; public string xclogourl { get; set; }<BR>}</P> <P>然后用以下方法:&nbsp;</P> <P>IDbConnection conn = new SqlConnection("Data Source=hp;Initial Catalog=picturedata;User ID=sa");<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DataContext ctx = new DataContext(conn);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Table&lt;XC&gt; xcx = ctx.GetTable&lt;XC&gt;();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var xcs = from x in xcx<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select new { x.xcname, x.xclogourl };<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return xcs;</P> <P>请问返回的XCS是什么类型,如果要转成datatable怎样转?</P> <P>我对泛型不太熟悉,请大家指教????</P>
iQingHan的主页 iQingHan | 初学一级 | 园豆:19
提问于:2008-03-07 15:13
< >
分享
所有回答(2)
0
该代码无法通过编译. 返回值的类型是 IEnumerable<某个匿名类型> 匿名类型无法作为函数返回值使用,只能在函数内部使用. 至于转DataTable,之所以使用ORM,就是为了强类型地使用数据,在这里如果把返回值转换成DataTable是绝对的倒退.真要这样的话,还不如不用Linq,直接用DbCommand来获取数据呢.
deerchao | 园豆:8367 (大侠五级) | 2008-03-07 15:18
0
楼上的说得非常有道理,如果你真的想返回,试试这样写: public class ReturnType { public ReturnType(string name, string logoUri) { // 此处省略若干字 } // 此处省略若干字 } public IEnumerable<ReturnType> GetPictures() { IDbConnection conn = new SqlConnection("Data Source=hp;Initial Catalog=picturedata;User ID=sa"); DataContext ctx = new DataContext(conn); Table<XC> xcx = ctx.GetTable<XC>(); IEnumerable<ReturnType> xcs = from x in xcx select new ReturnType(x.xcname, x.xclogourl); return xcs; } 至于Ajax,我就一点都不了解了。
Colin Han | 园豆:3041 (老鸟四级) | 2008-03-08 16:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册