cannot convert lambda expression to Func<T,TResult> while trying pass around an IQueryable<T>
So I posted this earlier.: IAsyncRepository or IObservableRepository for silverlight 4 + WCF Data Services
I've been working on an AsyncRepo for Silverlight so I've just got stuck at a small place.
The code is just for the explanation...scroll to bottom for the culprit code.
I have a repo defined like this:
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)
}
I'm using WCF Data Services + Linq to Entities. The first one works perfect like this:
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());
}
};
}
Now to the second method:
If you notice in the above method, I have a linq query I use to get the data. In my second repo method, I want to pass this query from the consumer into the metho开发者_如何转开发d.
So now..
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());
}
};
}
No problem so far... until...
I use it like this:
repo.GetAllFromQuery(
x => from p in x.Products where p.ID > 5 select p,
y => Assert.IsTrue(y.Count > 0));
This gives me :
cannot convert from 'lambda expression' to System.Func<MyEntities,IQueryable<Product.Calculator>>'
I will truly respect someone who gives me any solution. This has given me programmers block all day today!
EDIT: Okay, now the typo's fixed, try the non-query version (which I think is simpler anyway):
repo.GetAllFromQuery(
x => x.Products.Where(p => p.ID > 5),
y => Assert.IsTrue(y.Count > 0));
精彩评论