Linq LoadOptions problem

I

imbirek8

Hi,

When I do:

DataClassesDataContext db = new DataClassesDataContext (connectionString);
//do some select, read data from result
var options = new DataLoadOptions();
options.LoadWith<User>(u => u.UserType);
db.LoadOptions = options; // exception

I have an exception:
"Setting load options is not allowed after results have been returned from a
query".

It ridiculous that I can't add another LoadOption after select. What should
I do ? Should I add every LoadOptions on the start of my aplication ? But
what if in one select I would like to read User without UserType ?

Do you know any solution of this problem ?
 
F

Frans Bouma [C# MVP]

imbirek8 said:
Hi,

When I do:

DataClassesDataContext db = new DataClassesDataContext (connectionString);
//do some select, read data from result
var options = new DataLoadOptions();
options.LoadWith<User>(u => u.UserType);
db.LoadOptions = options; // exception

I have an exception:
"Setting load options is not allowed after results have been returned
from a query".

It ridiculous that I can't add another LoadOption after select. What
should I do ? Should I add every LoadOptions on the start of my
aplication ? But what if in one select I would like to read User without
UserType ?

Do you know any solution of this problem ?

There's no solution, as the design of the context + loadoptions is
wrong. The loadoptions belong to the query executed, as you want to do,
however, MS made them belong to the context. This means that ALL queries
executed on the context are using the same loadoptions.

So either use another context, or fetch the data up front.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
I

imbirek8

[...]
There's no solution, as the design of the context + loadoptions is wrong.
The loadoptions belong to the query executed, as you want to do, however,
MS made them belong to the context. This means that ALL queries executed
on the context are using the same loadoptions.

So either use another context, or fetch the data up front.
[...]

ok, but I would like to execute one query and than add to context another
loadoption and execute this query.
In this scenario I have the exception:
"Setting load options is not allowed after results have been returned from a
query".
Is it so important to add all load options at the beginning and than execute
all queries ?

thanks for help
 
F

Frans Bouma [C# MVP]

imbirek8 said:
[...]
There's no solution, as the design of the context + loadoptions is
wrong. The loadoptions belong to the query executed, as you want to
do, however, MS made them belong to the context. This means that ALL
queries executed on the context are using the same loadoptions.

So either use another context, or fetch the data up front.
[...]

ok, but I would like to execute one query and than add to context
another loadoption and execute this query.
In this scenario I have the exception:
"Setting load options is not allowed after results have been returned
from a query".
Is it so important to add all load options at the beginning and than
execute all queries ?

I tried to explain it to you why this happens. It's not my design, as I
think the LoadOptions design is pretty stupid, but it's what's there in
Linq to Sql, so you either have to live with it, or use something else.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top