2

I have something like this:

Func<Thread, bool> tmpFunc = thread => true;
threads = Threads.Where(tmpFunc).(...)

Now, when I do Threads.Where(thread => true).(...) everything is okay, but using variable in .Where() crashes my application. Why? Am I doing something wrong?

Okay, so here is the code that reproduces the error:

var threads = context.Categories
    .Where(c => c.Name == variable)
   .Select(c => new
   {
       threads = c.Threads
           .Where(tmpFunc)
           .OrderByDescending(t => t.DateCreated)
           .Skip(threadsToSkip)
           .Take(threadsPerPage)
           .Select(t => new
           {
               t,
               CategoryName = t.Category.Name,
               AuthorName = t.Author.UserName, 
               t.Posts.Count,
               LastPost = t.Posts
                   .OrderByDescending(post => post.DateCreated)
                   .Select(p => new{p.Author.UserName, p.DateCreated})
                   .FirstOrDefault()
            }),
            c.Threads.Count
    }).Single();

And the error it gives me is internal .net framework data provider error 1025

Flagbug
  • 2,093
  • 3
  • 24
  • 47
ojek
  • 9,680
  • 21
  • 71
  • 110
  • 3
    can you post an example of something that causes the crash? – Erix Jan 06 '13 at 17:44
  • 2
    Crashes your application how? Which exception do you get? Do you ever modify the `tmpFunc` variable? Can you post a short code sample that can be compiled and reproduces the problem? – sepp2k Jan 06 '13 at 17:44
  • Edited my post. I realize that this is much code, i am trying to shorten it abit now, but can't since such errors like this one with Func<> are ocurring. – ojek Jan 06 '13 at 17:50
  • Format that code as well please .. nobody wants to scroll so far to the right just to read your code.. – MethodMan Jan 06 '13 at 17:54

3 Answers3

3

My assumption is that there is no translatiin of a custom func to sql and the func is used in the projection. Although I would expect a "no sql translation for ... exists" exception.

To fix this, you could try declare your func as

Expression<Func<Thread, bool>> tmpFunc = thread => true;
threads = Threads.Where(tmpFunc).(...)
Wiktor Zychla
  • 47,367
  • 6
  • 74
  • 106
3

Try using an Expression instead of the Func directly:

Expression<Func<Thread, bool>> tmpFuncExpr = thread => true;

While Linq2Objects will be happy with the Func, Linq2Sql will not.

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • Just did this right now but i got this error: http://i.imgur.com/Fuvmb.jpg And have no idea what to do now... – ojek Jan 06 '13 at 18:05
  • 1
    @ojek - I am not an expert with EF, but can you try `c.Threads.AsQueryable().Where(tmpFunc)` instead of `c.Threads.Where(tmpFunc)` – manojlds Jan 06 '13 at 18:11
  • Oh. Okay, that works. Thanks. But... What is the difference between AsQueryable and AsEnumerable? – ojek Jan 06 '13 at 18:16
  • @Ojek - http://stackoverflow.com/questions/252785/what-is-the-difference-between-iqueryablet-and-ienumerablet – manojlds Jan 06 '13 at 18:17
2

When you're using the Entity Framework, any predicate passed to the EF's IQueryable has to be the Expression<Func<>>.

Check this answer stackoverflow.com/questions/11990158...

Community
  • 1
  • 1
Wolf
  • 2,150
  • 1
  • 15
  • 11