When i execute linq qeury like this

resource.Values.Single(value => value.Language.ID == 1051 && value.Value == "Zrušiť");

it throws InvalidOperationException with message 'Sequence contains no elements'. But when i put expression used in Single method into local Func variable:

Func<ResourceValue, bool> predicate =
  value => value.Language.ID == 1051 && value.Value == "Zrušiť";

and call

var item = resource.Values.Single(predicate);

then 1 record is returned without error. Am i missing something or is this a bug? If you need sample app i have one for you.

asked May 27 '11 at 06:53

Peter%20%C5%A0ulek's gravatar image

Peter Šulek
492313236

Peter, let's start with the sample. You know the e-mail.

(May 27 '11 at 06:57) Dmitri Maximov Dmitri%20Maximov's gravatar image

Send it to support at x-tensive.com

(May 27 '11 at 07:08) Peter Šulek Peter%20%C5%A0ulek's gravatar image

Got it. Will investigate on the next week.

(May 27 '11 at 07:48) Dmitri Maximov Dmitri%20Maximov's gravatar image

One Answer:

Peter,

when you use Func, it is applied to Enumerable, while the first case is applied to IQueryable and is translated by DataObjects.Net to SQL.

So the question is: why translation to SQL results into 0 items while applying the predicate to enumerable results into 1. I suppose that the reason might be in string comparison on database server you use. Try capturing SQL query with SQL Profiler and execute it without DataObjects.Net. What results you will get?

You might need to modify the collation of your database.

answered May 27 '11 at 09:07

Dmitri%20Maximov's gravatar image

Dmitri Maximov
22111211

edited May 27 '11 at 09:08

That make sense i will try to capture SQL query and let you know.

(May 27 '11 at 09:17) Peter Šulek Peter%20%C5%A0ulek's gravatar image

It makes this sql query:

SELECT TOP 2 [a].[Resource.ID], [a].[Language.ID], 103 AS [TypeId], [a].[Value] FROM [dbo].[ResourceValue] [a] WHERE (([a].[Resource.ID] = 1) AND (([a].[Language.ID] = 1051) AND ([a].[Value] = 'Zrušiť')))

Which returns 0 records, but when i change part [a].[Value] = 'Zrušiť' to [a].[Value] = N'Zrušiť' it works.

(May 27 '11 at 09:21) Peter Šulek Peter%20%C5%A0ulek's gravatar image

It seems that the translation of string constants should be fixed on our side. Try using the following approach for now (this will result into SQL command with parameter):

string value = "Zrušiť";
resource.Values.Single(value => value.Language.ID == 1051 && value.Value == value);
(May 27 '11 at 09:26) Dmitri Maximov Dmitri%20Maximov's gravatar image

Hack with local variable helps, but it is not good for our project in production. How hard will be to fix this? Still i have such "inline" strings only in unit-tests, because in application these strings always goes from some field/property/variable, but still.

(May 27 '11 at 09:35) Peter Šulek Peter%20%C5%A0ulek's gravatar image
1

This won't take too much. I suppose, during the next week I'll fix this.

(May 27 '11 at 09:37) Dmitri Maximov Dmitri%20Maximov's gravatar image

The bug is fixed, the binaries will be published soon

(May 31 '11 at 09:46) Dmitri Maximov Dmitri%20Maximov's gravatar image
1

The binaries are published, nightly build #7571

(May 31 '11 at 11:30) Dmitri Maximov Dmitri%20Maximov's gravatar image

Thanks for fix, will try it!

(May 31 '11 at 12:32) Peter Šulek Peter%20%C5%A0ulek's gravatar image

build #7571 fixes the bug, thanks!

(Jun 01 '11 at 02:22) Peter Šulek Peter%20%C5%A0ulek's gravatar image
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA