SQL Joins in Entity Framework

I am new to much of the .net entity framework and have been trying to understand how to do joins on data. I am used to using sql joins in ADO but it appears that is a thing of the past. If there is a better way to join tables, specifically on text fields with out a FK that would be great.

I have two different classes for the data:

public class App{
public int ID {get; set;}
public string Name {get; set;}
public string Location1 {get; set;}
public string Location2 {get; set;}
public string Location3 {get; set;}
}

public class Location{
public  LocationID {get; set;}
public string Location {get; set;}
public string Site
}

and i am trying to join the tables by:
public list<App> AppList {get; set;}
var querry = (from a in _Context.App join l in _Context.Location on a.Location1 equals l.Location OR a.Location2 equals l.Location OR a.Location3 equals l.Location
select new { a.Name, l.Site}).ToList();
AppList = querry;


Thank you for any direction you could provide.
Justin HullAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

louisfrCommented:
In Linq, join can only handle one equality comparison on one property or a group of properties. That handles AND comparisons.
If you want to OR comparisons, you need to do a cross-join and use where.
var querry = (from a in _Context.App
              from l in _Context.Location
              where a.Location1 == l.Location || a.Location2 == l.Location || a.Location3 == l.Location
              select new { a.Name, l.Site });

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
louisfrCommented:
Alternatively, you could define your own join method (I renamed your class to LocationX because you cannot have a property with the same name as the class):
    public static class LinqMethods
    {
        public static IEnumerable<T> Join<T>(this IEnumerable<App> apps, IEnumerable<LocationX> locations,
                                            Func<App,App> appSelector,Func<LocationX,LocationX> locationSelector,
                                            Func<App,LocationX,T> resultSelector)
        {
            foreach (var a in apps.Select(appSelector))
            {
                foreach (var l in locations.Select(locationSelector))
                {
                    if (a.Location1 == l.Location || a.Location2 == l.Location || a.Location3 == l.Location)
                    {
                        yield return resultSelector(a, l);
                    }
                }
            }
        }
    }

Open in new window

to join on the object themselves in Linq:
var querry = (from a in _Context.App
              join l in _Context.Location on a equals l
              select new { a.Name, l.Site });

Open in new window

0
Justin HullAuthor Commented:
Thank you for helping
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.