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

silverlight 里DataGrid 绑定Linq语句问题
单击BUTTON把vehicle 表的数据显示在datagrid控件中,能显示,但是我不想返回整表信息,而是返回表的两个字段信息var quer = from c in proxy.Vehicle select new { id=c.V_Id,noo=c.V_No}; 改成这样后就会有以下错误,请问这是什么原因



无法将类型为“DataServiceOrderedQuery[<>f__AnonymousType0`2[System.Int32,System.String]]”的对象强制转换为类型“System.Data.Services.Client.DataServiceQuery`1[Oil.ServiceReference1.Vehicle]”。

然后下面这两个语句能不能帮解释下,主要dataservicequery后面跟的<>和quer ,result.Asyncstate

DataSeviceQuery<Vehicle> userQuery = (DataServiceQuery<Vehicle>)quer;
 DataServiceQuery<Vehicle> query=(DataServiceQuery<Vehicle> )result.AsyncState ;

private void button1_Click(object sender, RoutedEventArgs e)
  {
  UserSystemOilEntities1 proxy = new UserSystemOilEntities1(new Uri("/wcfdata.svc", UriKind.Relative));
  var quer = from c in proxy.Vehicle select c;
  // var quer = from c in proxy.Vehicle select new { id=c.V_Id,noo=c.V_No};
   
  DataServiceQuery<Vehicle> userQuery = (DataServiceQuery<Vehicle>)quer;
  userQuery.BeginExecute(new AsyncCallback(OnLoadComplete),quer);
  }
  void OnLoadComplete(IAsyncResult result)
  {
  DataServiceQuery<Vehicle> query=(DataServiceQuery<Vehicle> )result.AsyncState ;
  dg2.ItemsSource = query.EndExecute(result).ToList();

  }

------解决方案--------------------
方法1.查询的时候,查询 vehicle 表中特定的两个字段
方法2.var quer = from c in proxy.Vehicle;你先试试这样是否能打印出结果集
------解决方案--------------------
var quer = from c in proxy.Vehicle select new { id=c.V_Id,noo=c.V_No}; 
=============
var quer = from c in proxy.Vehicle select new Vehicle{ id=c.V_Id,noo=c.V_No}; 

你的问题,是在你只查询特定字段时,用了匿名类导致的