views:

296

answers:

1

The code, with two Linq-to-SQL queries, that I am trying to optimize is below:

        var maxAInstant =
            (
                from a in db.As
                select a.Instant
            )
            .Max();
        var maxBInstant =
            (
                from b in db.Bs
                select b.instant
            )
            .Max();
        var interval = maxAInstant - maxBInstant;
        bool result = interval > new TimeSpan(0, 0, 1);

Can I obtain the result with a single Linq-to-SQL query?

+3  A: 

Try this:

bool result = (db.As.Max(a => a.Instant) - db.Bs.Max(b => b.instant)) > new TimeSpan(0,0,1);
Nick Berardi
I'm no expert, but doesn't it generates two sql queries?
Jader Dias
no way to stop it from generating two queries. because they are tables that cannot be joined together. from what I see. the trick to optimizing LINQ is making sure you can do it in SQL first.
Nick Berardi
then I'll have to write a SP to get this done in a single query... thanks
Jader Dias