Goo*_*ber 2 c# silverlight ienumerable entity-framework
目前我有一个使用RIA服务的C#Silverlight业务应用程序.该应用程序使用ADO.NET实体框架和域服务类托管在ASP.NET中,以读取和写入SQL Server数据库.
我的DomainServiceClass中有一个服务器端方法,它返回一个IEnumerable对象列表.在我的ApplicationName.Web.g.cs文件中,我也有一个自动生成的函数.在我的Silverlight客户端应用程序中,我希望能够在返回的对象列表上运行foreach循环.
public IEnumerable<Image> GetJobImages(string jobGuid)
{
var query =
(
from j in Context.Job
orderby (j.ShortCode)
where j.JobID.Equals(jobGuid)
join a in Context.Audit //.Distinct()
on j.JobID equals a.Job.JobID
join i in Context.Image
on a.Image.JobID equals i.JobID
select new Image
{
HighResUrl = i.HighResUrl,
LowResUrl = i.LowResUrl,
UploadDate = i.UploadDate
}).AsEnumerable();
return query;
}
Run Code Online (Sandbox Code Playgroud)
/// <summary>
/// Returns an EntityQuery for query operation 'GetJobImages'.
/// </summary>
public EntityQuery<Image> GetJobImagesQuery(string jobGuid)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("jobGuid", jobGuid);
return base.CreateQuery<Image>("GetJobImages", parameters, false, true);
}
Run Code Online (Sandbox Code Playgroud)
var context = dds.DomainContext as InmZenDomainContext;
foreach(var item in context.GetJobImagesQuery(currentJob.JobID.ToString())
{
item.etc.....//
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试如上所述调用此方法时,我收到错误:
'无法将System.Windows.Ria.Data.EntityQuery类型隐式转换为System.Collections.IEnumerable'.
要么
'不包含GetEnumerator的公共定义'.
在阅读并与他人交谈后,我被告知我需要更改AutoGenerated Function上的返回类型(我认为).
有没有人知道我可以在这个AutoGenerated EntityQuery上将返回类型更改为IEnumerable的方法?! - 或者以不同的方式解决这个问题?
/// <summary>
/// Returns an EntityQuery for query operation 'GetJobImages'.
/// </summary>
public IEnumerable<Image> GetJobImagesQuery(string jobGuid)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("jobGuid", jobGuid);
return base.CreateQuery<Image>("GetJobImages", parameters, false, true);
}
Run Code Online (Sandbox Code Playgroud)
(由于该方法是自动生成的,因此失败,它只是回到了构建解决方案时的状态.)
非常感谢.
问题是你在调用和IEnumerable集合之间错过了一步.
您需要执行EntityQuery并使用LoadOperation来获取结果.它并不像你想象的那么简单,这里有一个很好的论坛帖子,里面有一个如何处理事情的例子:
这是示例中的相关行:
EntityQuery query = Context.GetEmployeesQuery();
Context.Load<Employee>(query).Completed += (sender, args) => {
List<Employee> list = ((LoadOperation<Employee>)sender).Entities.ToList();
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12232 次 |
| 最近记录: |