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

关于entityFramework objectQuery<T> 类型转换的有关问题

2011-12-30 
关于entityFramework objectQueryT 类型转换的问题C# codeContextcontext new Context()string esql

关于entityFramework objectQuery<T> 类型转换的问题

C# code
Context  context = new Context();string esql = "select t.userID,t.userName from tb_user as t where t.sex=false";ObjectQuery<tb_user> typeRecord = ctx.CreateQuery<tb_user>(esql);ObjectResult<tb_user> typeResult = typeRecord.Execute(MergeOption.NoTracking);this.drpdownlist.datasource = typeResult.toList();this.drpdownList.datatextfield ="userID";this.drpdownList.datavaluefield ="userName";this.drpdownList.databind();

debug 到 数据源绑定的时候 发生错误 
The specified cast from a materialized 'System.Data.Objects.MaterializedDataRecord' type to the 'tb_user' type is not valid.

我换成
objectquery<dbDataRecord> typeRecord = ctx.CreateQuery<dbDataRecord>(esql);
ObjectResult<dbDataRecord> typeResult = typeRecord.Execute(MergeOption.NoTracking);
this.drpdownlist.datasource = typeResult.toList();
this.drpdownList.datatextfield ="userID"; //现在换成这个绑定错误 说是找不到userID,userName  
this.drpdownList.datavaluefield ="userName";
this.drpdownList.databind();


有没有高手遇到类似问题 请指教



[解决办法]
帮顶,蹭点分吧。

C# code
using (NorthwindEntities context = new NorthwindEntities()){    ObjectQuery<Category> query = context.Categories;    ObjectResult<Category> result = query.Execute(MergeOption.NoTracking);    DropDownList1.DataSource = result.ToList<Category>();    DropDownList1.DataTextField = "CategoryName";    DropDownList1.DataValueField = "CategoryID";    DropDownList1.DataBind();}
[解决办法]
csdn上问个问题确实垃圾,还不如百度知道
楼主用ef为啥还要用sql方式
CreateObjectSet<T>的方式会不会更好呢

热点排行