日期:2014-05-20  浏览次数:20794 次

关于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>的方式会不会更好呢