Solved

How to 'force' a type in LINQ?

Posted on 2010-11-16
2
361 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

789 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