Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How to resolve constant object property reference in DLinq expression

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andrus - 22 Mar 2008 22:30 GMT
The following code fails:

CompaniesDetails c2 = (from c in db.CompaniesDetails where c.CompanyID
              == comp.ID select c).Single();

"comp" is an instance of class Company:

public class Company
   {
      public int ID;
       {
         get;
         set;
       }

DLinq provider does not know what comp is in runtime.
So compiler must replace comp.ID with value when creating expression.
Any idea how to fix this ?

Andrus.
Jon Skeet [C# MVP] - 22 Mar 2008 22:57 GMT
> The following code fails:
>
[quoted text clipped - 13 lines]
> DLinq provider does not know what comp is in runtime.
> So compiler must replace comp.ID with value when creating expression.

No, the *compiler* doesn't have to do that at all. The *compiler* will
just be calling

db.CompaniesDetails.Where(c => c.CompanyID == comp.ID)

So, it's up to Where to understand the expression tree. If the DLinq
provider doesn't understand it, that sounds like a bug in DLinq to me.

Signature

Jon Skeet - <skeet@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

Andrus - 23 Mar 2008 10:19 GMT
> No, the *compiler* doesn't have to do that at all. The *compiler* will
> just be calling
[quoted text clipped - 3 lines]
> So, it's up to Where to understand the expression tree. If the DLinq
> provider doesn't understand it, that sounds like a bug in DLinq to me.

Provider gets string "comp.ID".
Lets assume that comp is local object name.
Provider does not have access to method local name table to resolve name
"comp".
As far as I know  local variable names like "comp" are not stored in
assembly and Linq does not pass such table to provider.

So provider cannot resolve the name "comp"  in any way.

Andrus.
Jon Skeet [C# MVP] - 23 Mar 2008 11:40 GMT
> > No, the *compiler* doesn't have to do that at all. The *compiler* will
> > just be calling
[quoted text clipped - 5 lines]
>
> Provider gets string "comp.ID".

No, the provider will get an expression tree, not a string, unless
something has already converted it into a string - which it certainly
shouldn't have done.

Put it this way - the compiler works exactly the same way with LINQ to
SQL, so do you believe *that* handles it? This is certainly an issue
with DbLINQ, *not* with the C# compiler.

Signature

Jon Skeet - <skeet@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

Andrus - 24 Mar 2008 00:22 GMT
Jon,

> Put it this way - the compiler works exactly the same way with LINQ to
> SQL, so do you believe *that* handles it? This is certainly an issue
> with DbLINQ, *not* with the C# compiler.

So you can confirm, that Linq-SQL can evaluate this expression, right ?
If So I must study passed expression tree a bit more.

Where to find some Linq provider sample code which resolves local object
references line comp.ID to values?

Andrus.
Jon Skeet [C# MVP] - 24 Mar 2008 08:22 GMT
> > Put it this way - the compiler works exactly the same way with LINQ to
> > SQL, so do you believe *that* handles it? This is certainly an issue
> > with DbLINQ, *not* with the C# compiler.
>
> So you can confirm, that Linq-SQL can evaluate this expression, right ?

It's as easy for you to try as it is for me - easier, as you know the
real context of your code. However, I've certainly done similar things
without any problem.

However, I can certainly confirm that the expression tree *won't* just
contain "comp.ID" as a string.

> If So I must study passed expression tree a bit more.
> Where to find some Linq provider sample code which resolves local object
> references line comp.ID to values?

I think you'll find it simple when you look at the actual expression
tree.

Signature

Jon Skeet - <skeet@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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.