score:0

        dim maxvalue = ctype((from p in db.products _
      where p.productid > 0 _
      select p.productid).max(),integer?)

score:0

holy crap what a pita

    dim maxvalue = (from p in db.products _
      where p.productid > 300 _ 
      select new with {.id=ctype(p.productid, integer?)}).max(function(p) p.id)

there has to be a better way, right?

this has the desired query plan and no error with null values, but can someone take a saw to it and clean it up?

score:0

c#

var maxvalue = nw.products
    .where(p => p.productid < 0)
    .select(p => p.productid)
    .defaultifempty(int.minvalue)
    .max();

vb

dim maxvalue = nw.products _
    .where(function(p) p.productid < 0) _
    .select(function(p) p.productid) _
    .defaultifempty(integer.minvalue) _
    .max()

score:0

how about a function that returns nullable? (sorry if the syntax isn't quite right.)

function getnullable(of t)(val as object)
    if (val is nothing) then 
        return new nullable(of t)()
    else
        return new nullable(of t)(directcast(val, t))
    end if
end function

dim maxvalue = (from p in products 
            where p.productid < 0 
            select getnullable(of integer)(p.productid)).max()

score:0

why not build the equivalent of an isnull check into the query?

dim maxvalue = (from p in products
                 where iif(p.productid=null, 0, p.productid) < 0
                 select p.productid)).max()

sorry if this doesn't work - i'm not actually testing it at this end, just throwing spaghetti on the wall!

score:1

short answer: you can.

and then the long answer:

the only way that i can see that you can do this, is to create the lambda containing the typeas conversion explicitly. you can use the following extension methods to help you here:

<extension()> _
public module typeasextensions
    <extension()> _
    public function selectas(of telement, toriginaltype, ttargettype)( _
        byval source as iqueryable(of telement), _
        byval selector as expression(of func(of telement, toriginaltype))) _
        as iqueryable(of ttargettype)

        return queryable.select(source, _
            expression.lambda(of func(of telement, ttargettype))( _
                expression.typeas(selector.body, gettype(ttargettype)), _
                selector.parameters(0)))
    end function

    <extension()> _
    public function selectasnullable(of telement, ttype as structure)( _
        byval source as iqueryable(of telement), _
        byval selector as expression(of func(of telement, ttype))) _
        as iqueryable(of ttype?)
        return selectas(of telement, ttype, ttype?)(source, selector)
    end function
end module

selectas will in result in a trycast(value, t) for any t, including integer?.

to use this, you would say

dim maxvalue = products _
               .where(function(p) p.productid < 0) _
               .selectasnullable(function(p) p.productid) _
               .max()

it ain't pretty, but it works. (this generates the same query as c# does.) as long as you don't call selectasnullable within a sub-query you're fine.

another option could be to use

dim maxvalue = (from p in products _
                where p.productid < 0 
                select p.productid) _
               .selectasnullable(function(id) id) _
               .max()

the problem with this is that you get a double select, i.e.,

from p in products 
where p.productid < 0 
select p.productid 
select p.productid as int?

in c# parlance. it's quote possible linq to sql still generate a subquery for this too.

anyway, for this you can create an additional extension method

<extension()> _
public function selectasnullable(of ttype as structure)( _
    byval source as iqueryable(of ttype)) _
    as iqueryable(of ttype?)
    return selectas(of ttype, ttype, ttype?)(source, function(x) x)
end function

simplifying the linq query further

dim maxvalue = (from p in products _
                where p.productid < 0 
                select p.productid) _
               .selectasnullable() _
               .max()

but as i said, this depends on the linq provider.


Related Query

More Query from same tag