2

I've created three table's name: emps, emp_project, emp_location

now i want to select one column from each table, but when im executing join query so far getting this this error:

The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'GroupJoin'.

the query which im executing is:

from e in Emp_info
from p in Emp_projects
join l in Emp_locations
    on new { e.User_id , p.Project_id  } equals new { l.User_id, l.Project_id  } into detail
from l in detail
select new
{
    e.Middlename,
    p.Project_name,
    l.Location
}; 
query.Dump("Join query");

Don't know which of the clauses is causing the error!

venerik
  • 5,766
  • 2
  • 33
  • 43
Prince AJ
  • 33
  • 1
  • 5
  • Possible duplicate of [How to do joins in LINQ on multiple fields in single join](http://stackoverflow.com/questions/373541/how-to-do-joins-in-linq-on-multiple-fields-in-single-join) – venerik Oct 14 '15 at 07:51
  • some how duplicate, but what im asking that i've three tables not two...am i wrong to think that linq can only join two table at a time?? – Prince AJ Oct 14 '15 at 07:57
  • I tried replicating and it works fine in my case (note, no db involved, just lists) – Johan Oct 14 '15 at 08:05

4 Answers4

0

My guess, is that the two anonymous types it is trying to compare aren't the same (also ensure that the properties are the same datatype).

Change

on new { e.User_id , p.Project_id  } equals new { l.User_id, l.Project_id  } into detail

To

 on new { UserId = e.User_id, ProjectId = p.Project_id  } equals 
    new { UserId = l.User_id, ProjectId = l.Project_id  } into detail
Johan
  • 8,068
  • 1
  • 33
  • 46
0

this works for me

void Main()
{
    var Emp_info = new List<Info>
        {
            new Info {Middlename = "Middlename",User_id = "1"}
        };
    var Emp_projects = new List<Project>
        {
            new Project{Project_id = "1",Project_name = "Project"}
        };
    var Emp_locations = new List<LocationInfo>
        {
            new LocationInfo{Location = "Location",Project_id="1",User_id = "1"}
        };

    /* your code */
    var query = from e in Emp_info
    from p in Emp_projects
    join l in Emp_locations
        on new { e.User_id , p.Project_id  } equals new { l.User_id, l.Project_id  } into detail
    from l in detail
    select new
    {
        e.Middlename,
        p.Project_name,
        l.Location
    }; 

    query.Dump("Join query");
    /* your code */
}

class Info
{
    public string User_id;
    public string Middlename;
}

class Project
{
    public string Project_id;
    public string Project_name;
}

class LocationInfo
{
    public string User_id;
    public string Project_id;
    public string Location;
}
manda
  • 228
  • 1
  • 10
0

So far i've come to this solution , by using more than one join at a time. don't is it the right practice or not.

var query=
        from e in Emp_info
        join l in Emp_locations on e.User_id  equals  l.User_id // first join
        join p in Emp_projects on l.Location_id equals p.Project_id    // second join
        select new
        {
            Name= e.Middlename,
            Project =  p.Project_name,
            Location = l.Location
        };

        query.Dump();
Prince AJ
  • 33
  • 1
  • 5
0

finally got the answer.. this is working perfectly as i want

      var query=
from e in db.emp_info
from p in db.emp_projects
join l in db.emp_locations
    on new { username= e.User_id ,project_id=p.project_id } equals new { username=l.User_id,project_id= l.Project_id  } into detail
from l in detail
select new
{
   e,p,l
};
            foreach (var q in query)
            {
                Console.WriteLine("{0} has done project like {1} in {2}",q.e.Middlename,q.p.project_name,q.l.location);
            }
            Console.ReadLine();
                }
                Console.ReadLine();
Prince AJ
  • 33
  • 1
  • 5
  • first problem which im facing is that the attributes property are different and the next problem is that im not using any variable for any of the column name to match it with the other column. i.e **usrname** for 'e.userid & l.user_id'... i hope and think now you can understand. – Prince AJ Oct 20 '15 at 07:00