Solved

Linq-to-SQL "order by" child table values and return custom type

Posted on 2010-11-18
2
1,140 Views
Last Modified: 2013-11-11
Simple set of 4 tables that link together: Projects -> Versions -> Marketshares -> Countries

Linq to SQL class has been set up to that each subsequent table as shown above is an entity set of the one before with ALL being one to many from parent to child except Marketshares -> Countries which is one to one.

If a list (of project) is returned then it's possible to iterate through the other tables on the linked values and all works well.

However, one particular query requires that I return only those projects where a particular element matches a filter setting on the marketshare table. This is fine and I can happily select the matching projects. Unfortunately I need to return a custom list and this means I need access to the values in marketshare and countries. But as these are one-to-many relationships, I cannot directly do something like:
p.project.version.marketshare.countries.countryname as I need the element index of some items such as p.project(0).versions(0).marketshare.countries.countryname but that is obviously not the right way to go about this...

So basically there is a value in marketshare called marketdestination and this links to an id in countries from which I pull the countryname. Assuming that I have the query correct (and the query below works for selection) then how do I specify that I want to sort the results by countryname from countries and return marketdestination and countryname in the query please?

thisList = (From p In pdc.tblProjects _
Where p.tblProjectVersions.Any(Function(t) t.tblProjectVersionMarketShares.Any(Function(tt) tt.marketDestination = lf.marketDestination))) _ And p.isDeleted = False _
Order By p.tblProjectVersions.FirstOrDefault.tblProjectVersionMarketShares.FirstOrDefault.tblCountries.CountryName Ascending _ 
Select New genListItem With {.listID = p.tblProjectVersions.FirstOrDefault.tblProjectVersionMarketShares.FirstOrDefault.marketDestination, .listValue = .tblProjectVersions.FirstOrDefault.tblProjectVersionMarketShares.FirstOrDefault.tblCountries.CountryName}).Distinct().ToList

Open in new window


I tried using "firstordefault" but it doesn't work...
0
Comment
Question by:carled
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
Stephan earned 500 total points
ID: 34214334
You are trying to use many-to-many relationships within linq-to-sql.... many-to-many is not supported, linq to entities has this possibility.

Try to devide your query using "var". I'm not really into the VB.NET so it is hard for me to read.

If you put your query till the orderby statement and put this in a separate var, then create the select from the var you created.

Take a look at this:
http://stackoverflow.com/questions/1097992/linq-to-entities-many-to-many-select-query
0
 
LVL 1

Author Closing Comment

by:carled
ID: 34214362
Many thanks, helped greatly!
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

734 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