1

I have, what I thought was a pretty straight-forward query.

In normal Sql this would read:

SELECT    [column names]
FROM      agentscheduledetail
WHERE     (date = '2012-07-04') AND 
           (
            exception = 'Break (No Sign Off)' OR
            exception = 'Break' OR
            exception = 'Break (Signed Out)'
           )

This returns approx 900 records.

However, when I try to enter this into my controller, I end up with around 300,000 records - so I think my AND and ORs are not working. I've tried Linqer, but can't get it to work (I'm aware this may not be actual LINQ but the equivalent query in VS - if there is a linq version... I'd be grateful for that too if possible).

My controller query is:

        var dte = DateTime.Today;

        return View(db.agentscheduledetails.Where
           (
            d => d.date == dte && d.agentName.StartsWith("ta") && 
             (
              d.exception == "Break (No Sign Off)" || 
              d.exception == "Break" || 
              d.exception == "Break (Signed Out)"
             )
            ).ToList()
           );

Can anyone either a) let me know where I'm going wrong with my && || (and/or), or b) is there a way of stepping through the code in VS, to see what the above query translates to in normal SQL so I can try to figure out where I'm going wrong?

Thanks for any help,

Mark

7
  • try using .Contains() to check string Commented Jul 4, 2012 at 8:45
  • 1
    You haven't specified whether you're using LINQ to SQL or Entity Framework - but either way, yes, there are ways of looking at the generated SQL. See DataContext.Log for example in LINQ to SQL. Commented Jul 4, 2012 at 8:46
  • Setup the DB context to output sql queries to a log file or the debugger windows. Check that's correct. Commented Jul 4, 2012 at 8:46
  • Hi - it is the Entity Framework - I generated my classes, and clicked Add controller in VS, and let it build the default controller code using EF. All I am trying to do is filter the basic query it generates in the GET method. Contains returns the same. How do you setup the DB Context to output queries - the DBContext was also setup automatically by VS when I created the controller. Thanks. Commented Jul 4, 2012 at 8:52
  • if you do create a query object e.g. var q=...where... (missing out the ToList() on the end. Visual Studio debugger will show you the Generated SQL if you inspect the value. Commented Jul 4, 2012 at 8:53

2 Answers 2

1

The following is perhaps a simplified version of what you are trying to do, also your LINQ contains an additional statement compared to the SQL where it is comparing the agent name?

var currentDate = DateTime.Today;
var exceptionTypes = new List<string>() { "Break (No Sign Off)", 
                                          "Break", "Break (Signed Out)" };

db.agentscheduledetails.Where(d => d.date == currentDate && 
                                           exceptionTypes.Contains(d.exception));

One thing that you could try is getting hold of a copy of LinqPad, this will let you run your LINQ statement against a database and will show you what the generated SQL statement is.

Sign up to request clarification or add additional context in comments.

2 Comments

hi - thanks - that works perfect. Can you not use && and || in the statements I was making? (I'll mark as answer as soons as SO allows) - cheers, Mark
If I run something similar in LinqPad then I get the desired result and the correct SQL. I'm not sure about your set up though, if it's the Entity Framework then I'm not sure what will be happening, but in normal Linq to SQL it should work fine? This method tends to be the preferred though as it's generally simpler to write.
0

Aside from anything else,

    d.agentName.StartsWith("ta")

does not appear in your original sql...?

1 Comment

you're correct - I added that to see if it would filter the records anymore - it didn't...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.