Home All Groups Group Topic Archive Search About

problem with var in 3.5

Author
25 Nov 2007 8:52 PM
dan
Hello,
i don't know whether you can answer to questions about issues in 3.5, but
i'll ask anyway.
i have this code:

var material = from materials in myDC.Materials
                    where materials.Name == materialName
                    select materials;

it is supposed to load 1 object.
how do i know if "material" is loaded or is null or smth like this?
or should i use foreach every time? because in this case, material is
IQueriable<Material>.

or i can write an extension method, but i have to write this for all my
domain objects...
so, how can i do it in a simpler way?

Thank you,
Dan

Author
25 Nov 2007 9:10 PM
Jon Skeet [C# MVP]
dan <d@d.d> wrote:
> i don't know whether you can answer to questions about issues in 3.5, but
> i'll ask anyway.
> i have this code:
>
> var material = from materials in myDC.Materials
>                     where materials.Name == materialName
>                     select materials;
>
> it is supposed to load 1 object.
>
> how do i know if "material" is loaded or is null or smth like this?
> or should i use foreach every time? because in this case, material is
> IQueriable<Material>.

Options are:

FirstOrDefault() - there might be 0 or 1 matches
First() - there might be more than one match, just use the first
Single() - there should be exactly one match, throw otherwise

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk
Author
26 Nov 2007 5:29 PM
dan
Show quote
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MPG.21b3f527fbdd9d07668@msnews.microsoft.com...
> dan <d@d.d> wrote:
>> i don't know whether you can answer to questions about issues in 3.5, but
>> i'll ask anyway.
>> i have this code:
>>
>> var material = from materials in myDC.Materials
>>                     where materials.Name == materialName
>>                     select materials;
>>
>> it is supposed to load 1 object.
>>
>> how do i know if "material" is loaded or is null or smth like this?
>> or should i use foreach every time? because in this case, material is
>> IQueriable<Material>.
>
> Options are:
>
> FirstOrDefault() - there might be 0 or 1 matches
> First() - there might be more than one match, just use the first
> Single() - there should be exactly one match, throw otherwise

Thank you!

I used also
material.Count<Material>() < 1

Dan

AddThis Social Bookmark Button