Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I've been trying to transform a SQL Statement (below) to a LINQtoSQL statement. I developed a horrible workaround, but it's incredible inefficient. Does anyone know how to do it without all horribleness?

SQL Select Statement:
SQL
SELECT DISTINCT locationGroups.*
FROM locationGroups
JOIN locations
	ON locationGroups.locationGroupID = locations.locationGroup
JOIN locationRegions
	ON locations.region = locationRegions.regionID
WHERE locationRegions.regionID = 34


My horrible c# code:
C#
public List<locationGroup> GetLocationGroups(locationRegion locationRegion)
        {
            #region Inefficent code

            using (var context = new Entities())
            {
                location[] lA = context.locations.Where(location => location.region == locationRegion.regionID).ToArray();

                List<locationGroup> lG = new List<locationGroup>();

                foreach (location l in lA)
                {
                    if (lG.Any(list => list.locationGroupID == l.locationGroup) == false)
                        lG.Add(context.locationGroups.FirstOrDefault(locationGroup => locationGroup.locationGroupID == l.locationGroup));
                }

                return lG;
            }

            #endregion
        }
Posted
Updated 25-Jul-13 2:20am
v3

I went to Mycroft Holmes suggested site and after awhile I finally built something that worked. I posted the solution below for reference to anyone else who needs help on this issue.

C#
using (var context = new iomniEntities())
            {
                List<locationGroup> lLG = new List<locationGroup>();
                lLG = (from lG in context.locationGroups
                       join l in context.locations on lG.locationGroupID equals l.locationGroup
                       join lR in context.locationRegions on l.region equals lR.regionID
                       where (l.locationTypeID == 9 || l.locationTypeID == 11) && l.active == true && lR.regionID == locationRegion.regionID
                       select lG).Distinct().ToList<locationGroup>();

                return lLG;

            }
 
Share this answer
 
v2
Comments
Maciej Los 1-Aug-13 16:00pm    
5ed!
Kyle Gottfried 2-Aug-13 13:39pm    
what?
Maciej Los 2-Aug-13 15:08pm    
I vote-up your answer ;)
Kyle Gottfried 2-Aug-13 15:38pm    
oh, I get it.
Mycroft Holmes 2-Aug-13 20:06pm    
And some idiot down voted you answer!
Good for you getting the answer and even better for posting the solution. Personally I hate linq but ut is just too useful to ignore.
I find this site most useful when faffing around with linq 101 Linq samples[^]

There are some join examples in there
 
Share this answer
 
Comments
Maciej Los 2-Aug-13 15:09pm    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900