From: Matthijs de Z on
Hi,

I want to run a linq query on an entity table linke this:

var myGroupNameIDvar = (from selecteddonwloadgroup in
db.SelEcTedDonWLoadGroup
where
selecteddonwloadgroup.DownloadGroupName == "test"
select
selecteddonwloadgroup).First();

if the query is able to return a row, there's no problem. But if there
is no data to return, because "test" doesn't exist, I get the error
"Operation is not valid due to the current state of the object".

When I use singeOrDefault() teher is no problem. But default will be 0
in this case and that could be linked to something else. So I would
like to first check if the query returns data, if so..I'll get the ID
(which is an uint).

Can anybody help me out how to work around this problem.
Regards,

Matthijs
From: Willem van Rumpt on
On 28-5-2010 11:47, Matthijs de Z wrote:
> Hi,
>
> I want to run a linq query on an entity table linke this:
>
> var myGroupNameIDvar = (from selecteddonwloadgroup in
> db.SelEcTedDonWLoadGroup
> where
> selecteddonwloadgroup.DownloadGroupName == "test"
> select
> selecteddonwloadgroup).First();
>
> if the query is able to return a row, there's no problem. But if there
> is no data to return, because "test" doesn't exist, I get the error
> "Operation is not valid due to the current state of the object".
>
> When I use singeOrDefault() teher is no problem. But default will be 0
> in this case and that could be linked to something else. So I would
> like to first check if the query returns data, if so..I'll get the ID
> (which is an uint).
>

First of all, how does that query return 0? From the (very) limited
sourcecode you provided, "selecteddonwloadgroup" does not appear to be a
numeric type.

Second, I seem to recall that SingleOrDefault is not supported by Linq
to Entities.

Third, "First" and "Single" are *not* the same. "First" retrieves the
first applicable element, "Single" retrieves the *only* element, and
asserts that there also only *is* one element.

It seems that FirstOrDefault (or SingleOrDefault, whatever is
appropriate) would still be the way to go. From what you've shown,
"selecteddonwloadgroup" (while you're at it, also check the spelling ;)
) seems to be a reference type. When null is returned, there is no such
instance.

--
Willem van Rumpt
From: Matthijs de Z on
Hi Willem,

bedankt dat je er even naar wilde kijken.
(thanks for helping out).

> First of all, how does that query return 0? From the (very) limited
> sourcecode you provided, "selecteddonwloadgroup" does not appear to be a
> numeric type.

selecteddownloadgroup is a mysql mapped table (using dblinq)
this is the table:

CREATE TABLE `selecteddonwloadgroup`
(
`DownloadGroupID` int(11) unsigned NOT NULL
AUTO_INCREMENT,
`DownloadGroupName` varchar(45) COLLATE
latin1_general_ci DEFAULT
NULL,
PRIMARY KEY
(`DownloadGroupID`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT
CHARSET=latin1 COLLATE=latin1_general_ci CHECKSUM=1 DELAY_KEY_WRITE=1
ROW_FORMAT=DYNAMIC


> Second, I seem to recall that SingleOrDefault is not supported by Linq
> to Entities.
>
> Third, "First" and "Single" are *not* the same. "First" retrieves the
> first applicable element, "Single" retrieves the *only* element, and
> asserts that there also only *is* one element.
>
> It seems that FirstOrDefault (or SingleOrDefault, whatever is
> appropriate) would still be the way to go. From what you've shown,
> "selecteddonwloadgroup" (while you're at it, also check the spelling ;)
> ) seems to be a reference type. When null is returned, there is no such
> instance.

when I use firstOrDefault like this:

uint myGroupNameIDvar = (from selecteddownloadgroup in
db.SelEcTedDonWLoadGroup.AsEnumerable()
where
selecteddownloadgroup.DownloadGroupName == "test2"
select
selecteddownloadgroup.DownloadGroupID).FirstOrDefault();

I will still get 0 when there is not match for the where. While 0
could actually be an ID of another DownloadGroupID. That's the
problem... :-(
regards,

Matthijs

>
> --
>    Willem van Rumpt- Tekst uit oorspronkelijk bericht niet weergeven -
>
> - Tekst uit oorspronkelijk bericht weergeven -

From: Matthijs de Z on
I solved it like this:

var myGroupNameIDvar = (from selecteddownloadgroup in
db.SelEcTedDonWLoadGroup
where
selecteddownloadgroup.DownloadGroupName == "test"
select
selecteddownloadgroup).FirstOrDefault();

if (myGroupNameIDvar!=null)
{
uint test = myGroupNameIDvar.DownloadGroupID;
//and do the rest
}
From: Willem van Rumpt on
On 28-5-2010 14:22, Matthijs de Z wrote:
> Hi Willem,
>
> bedankt dat je er even naar wilde kijken.
> (thanks for helping out).
>

No problem :)

>
> when I use firstOrDefault like this:
>
> uint myGroupNameIDvar = (from selecteddownloadgroup in
> db.SelEcTedDonWLoadGroup.AsEnumerable()
> where
> selecteddownloadgroup.DownloadGroupName == "test2"
> select
> selecteddownloadgroup.DownloadGroupID).FirstOrDefault();
>
> I will still get 0 when there is not match for the where. While 0
> could actually be an ID of another DownloadGroupID. That's the
> problem... :-(

Which is why it's important to post actual code.
This is your original code fragment:

var myGroupNameIDvar = (from selecteddonwloadgroup in
db.SelEcTedDonWLoadGroup
where
selecteddonwloadgroup.DownloadGroupName == "test"
select
selecteddonwloadgroup).First();

(and while I'm at it: "db.SelEcTedDonWLoadGroup"... What's up with this
name? Any reason you're obfuscating your own source?)

If "0" is a valid Id in your setup, then the query as you've provided it
is not usable. If you specify more requirements and details, then
presumably a more suitable solution can be worked out. But as it stands,
more info is required. Without additional information , the easy way out
is to just fetch the entire entity and see if it comes up as a null
result. Then do something based on that.

--
Willem van Rumpt