3

I have a Windows forms application (.NET 4.0) running with a Sql Server CE 3.5 database, which I access via an EF connection.

Here is my initial query, which returns two results:

var list = db.UserPresentation
                         .Select(up => new
                         {
                             UserPresentationID = up.UserPresentationID,
                             PresentationName = up.PresentationName,
                             DateRequested = up.DateRequested,
                             Edit = string.Empty,
                             Delete = string.Empty,
                             Download = string.Empty
                         })
                         .OrderByDescending(up => up.DateRequested)
                         .ToList();

Now I introduce an external variable and a where clause, and it returns zero results. If I run this same code in LinqPad, it returns 2 results.

 int userID = 2;

            // load list of user presentations
            var list = db.UserPresentation
                         .Where(up => up.UserID == userID)
                         .Select(up => new
                         {
                             UserPresentationID = up.UserPresentationID,
                             PresentationName = up.PresentationName,
                             DateRequested = up.DateRequested,
                             Edit = string.Empty,
                             Delete = string.Empty,
                             Download = string.Empty
                         })
                         .OrderByDescending(up => up.DateRequested)
                         .ToList();

Now I hardcode the userid inside the query, and it returns two results again:

var list = db.UserPresentation
                         .Where(up => up.UserID == 2)
                         .Select(up => new
                         {
                             UserPresentationID = up.UserPresentationID,
                             PresentationName = up.PresentationName,
                             DateRequested = up.DateRequested,
                             Edit = string.Empty,
                             Delete = string.Empty,
                             Download = string.Empty
                         })
                         .OrderByDescending(up => up.DateRequested)
                         .ToList();

I'm really stumped. Any idea what's going on here?

draconis
  • 599
  • 1
  • 5
  • 16
  • You haven't actually told us what the problem is (that I can see). Is returning the two results what you want? – Ray Nov 11 '11 at 10:57
  • The query should return two results, yes. But the broader problem is, why doesn't the query work if I use a variable instead of a hard-coded value in the where clause?? – draconis Nov 11 '11 at 10:59
  • 3
    There is no reason why it should not work. Perhaps you have another `userId` variable with different casing. – leppie Nov 11 '11 at 11:03
  • @draconis you might need to do some experimentation. I agree with leppie that the problem must be with something we can't see. – Ray Nov 11 '11 at 11:08
  • Also, speaking of casing on names: http://stackoverflow.com/questions/264823/whats-the-proper-naming-convention-for-a-property-id-id-or-id – Merlyn Morgan-Graham Nov 11 '11 at 11:12
  • I usually work with web apps, not desktop apps, so I thought I might be missing something obvious. But I agree, there is nothing wrong here. I'll experiment some more and let you know. Thanks. – draconis Nov 11 '11 at 11:12
  • I'd check the type of up.UserID and also the corresponding database type to see if they all match 'int', that's the only thing I can think of – James Ellis-Jones Nov 11 '11 at 12:10
  • Did you check any SQL that runs? – Gert Arnold Nov 17 '11 at 21:45

2 Answers2

0

Is UserID nullable?

If so be sure to do .Where(up => up.UserID.HasValue && up.UserID.Value == userID)

I had something similar with a nullable datetime once

andrewtatham
  • 1,093
  • 9
  • 10
-1

Have you tried assigning the same hard coded value inside your variable? My guess is that the value in your variable is not found among your data, that is if you are sure that the variable name is correct.

Chibuzo
  • 6,112
  • 3
  • 29
  • 51