【问题标题】:cannot convert lambda expression to Func<T,TResult> while trying pass around an IQueryable<T>尝试传递 IQueryable<T> 时无法将 lambda 表达式转换为 Func<T,TResult>
【发布时间】: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&lt;MyEntities,IQueryable&lt;Product.Calculator&gt;&gt;'

我会真正尊重给我任何解决方案的人。这让我今天的程序员整天都在阻塞!

【问题讨论】:

  • 对于任何对此设计发表评论的人以及是​​否可以或我可以做得更好的人。

标签: c# linq entity-framework-4 linq-to-entities wcf-data-services


【解决方案1】:

编辑:好的,现在错字已修复,尝试非查询版本(我认为它更简单):

repo.GetAllFromQuery(
    x => x.Products.Where(p => p.ID > 5),
    y => Assert.IsTrue(y.Count > 0));

【讨论】:

  • 啊!对不起..那是一个简化出错了......错字已修复。当我将 p 悬停在 Visual Studio 中时,它给了我 ? range variable,而不是巧妙地找出类型。
  • @giddy:MyEntities.Products 的类型是什么?非查询版本好用吗?
  • 不!还没有运气。当我输入 x.Products.. 我得到智能感知直到那里.. 之后智能感知开始折腾。但是没有。仍然给我同样的错误。我是在混淆类型..还是定义错误..
  • @giddy:您是否缺少包含此代码的文件顶部的using System.Linq;?尝试编译非查询版本时的错误信息是什么?
  • @jon 是的。我确实有System.Ling ....Error (1) Invalid argument... (2) 无法将 lambda 表达式转换为...。(与以前相同的错误没有变化)可能是这个调用是在 Silverlight Test 应用程序中,我没有对实体框架的引用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-15
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多