Solved

How to 'force' a type in LINQ?

Posted on 2010-11-16
2
359 Views
Last Modified: 2012-05-10
Here is a LINQ query that works without the second JOIN clause (3rd line) along with the statements that provide the collections used in the query.

The problem is on the 3rd line:

    join Location in _locations on Location.Id equals Device.LocationId

While LINQ is happy parsing the private type Camera and Device, it has problems with the private type Location... behind the word 'on' it wants to use the Point type Form.Location instead of my private type as a range variable although it correctly uses the private type as a range variable earlier in the line just after 'join'.

Is there some syntax I can use to instruct the parser to use the private type in the second position as well?  No, I can't change the name of Location, it is coming from a dll I don't have the source code for.

(BTW: calling the second position out by its full name 'Client.ClientEntities.Location' doesn't help... the parser still doesn't see it as a range variable )


I am not very good with the non-LINQ style of these queries (i.e. the Collection.Select() style) so if there is not a way to force the parser, could someone do me a huge favor and convert my LINQ query to the Select() syntax please?  I have others of these to do, but one good example will get me all that I need.

Thank you for your time,
Dave





var _cameras = DAL.Cameras.ToList();  // Collection of type Camera
var _devices = DAL.Devices.ToList();  //Collection of type Device
var _locations = DAL.Locations.ToList();  //Collection of type Location
                                
var fullCams = from Camera in _cameras
                join Device in _devices on Camera.DeviceId equals Device.Id
                join Location in _locations on Location.Id equals Device.LocationId
                select new _Device { GUID = Device.Id, DisplayName = Device.DisplayName, Type = "Camera" };

Open in new window

0
Comment
Question by:davecove
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 34147547
I think this is what you are asking, but I may have the type wrong as I tried to infer it from your question. In the event that I am wrong, change "Client.ClientEntities.Location" below to the type name of the type of objects stored in _locations.
var fullCams = from Camera in _cameras
                join Device in _devices on Camera.DeviceId equals Device.Id
                join Client.ClientEntities.Location Location in _locations on Location.Id equals Device.LocationId
                select new _Device { GUID = Device.Id, DisplayName = Device.DisplayName, Type = "Camera" };

Open in new window

0
 
LVL 33

Assisted Solution

by:Todd Gerbert
Todd Gerbert earned 100 total points
ID: 34147551
Use a fully qualified class name to differentiate between classes with like names, e.g.

Join MyNamespace.MyClassName Location on Location.Id etc
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question