【发布时间】:2011-03-18 14:59:55
【问题描述】:
所以我之前发布了这个: IAsyncRepository or IObservableRepository for silverlight 4 + WCF Data Services
我一直在为 Silverlight 开发 AsyncRepo,所以我只是被困在一个小地方。
代码仅用于解释...滚动到底部查看罪魁祸首代码。
我有一个这样定义的仓库:
public interface IAsyncRepository<T> where T : class
{
void GetById(int id, Action<T> callback);
void GetAllFromQuery(Func<MyEntities, IQueryable<Product>> funcquery, Action<IList<Calculator>> callback)
}
我正在使用 WCF 数据服务 + Linq to Entities。第一个完美运行如下:
public void GetById(int id, Action<Product> callback)
{
MyEntities dat = new MyEntities(new Uri(..url..));
var query = from c in dat.Products where c.ID == id select c;//WATCH THIS
allQuery = new DataServiceCollection<Product>(dat);
allQuery.LoadAsync(query);
allQuery.LoadCompleted += (obj, evt) =>
{
if (allQuery == null)
{
callback(null);
}
else
{
callback(allQuery.FirstOrDefault());
}
};
}
现在到第二种方法:
如果您注意到上述方法,我有一个 linq 查询 用于获取数据。在我的第二个 repo 方法中,我想将此查询从消费者传递到方法中。
那么现在..
public void GetAllFromQuery(Func<MyEntities, IQueryable<Product>> funcquery, Action<IList<Product>> callback)
{
MyEntities dat = new MyEntities(..uri..);
allQuery = new DataServiceCollection<Product>(dat);
allQuery.LoadAsync(funcquery(dat));
allQuery.LoadCompleted += (obj, evt) =>
{
if (allCalcQuery == null)
{
callback(null);
}
else
{
callback(allQuery.ToList());
}
};
}
到目前为止没问题...直到...
我是这样使用的:
repo.GetAllFromQuery(
x => from p in x.Products where p.ID > 5 select p,
y => Assert.IsTrue(y.Count > 0));
这给了我:
cannot convert from 'lambda expression' to System.Func<MyEntities,IQueryable<Product.Calculator>>'
我会真正尊重给我任何解决方案的人。这让我今天的程序员整天都在阻塞!
【问题讨论】:
-
对于任何对此设计发表评论的人以及是否可以或我可以做得更好的人。
标签: c# linq entity-framework-4 linq-to-entities wcf-data-services