Click here to Skip to main content
15,881,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables with common a field called imo_no.

I want to join these two tables, but how can I do that?

using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Collections.Generic;

namespace SilverlightBingMapControl.Web
{
    [ServiceContract(Namespace = "")]
    [SilverlightFaultBehavior]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PositionService
    {
        [OperationContract]
        List<it_position> GetAllPositions()
        {
            PositionDataClassesDataContext context = new PositionDataClassesDataContext();

            var result = from positions in context.it_positions select positions;
            return result.ToList();
        }


        [OperationContract]
        List<it_vessel> GetAllVessels()
        {
            PositionDataClassesDataContext ser = new PositionDataClassesDataContext();
            var res = from vessels in ser.it_vessels select vessels;

            return res.ToList();
        }
    }
}



Changes:
I tried this, but got error:
XML
[OperationContract]
List<it_vessel> GetAllInitialize()
{
    PositionDataClassesDataContext context = new PositionDataClassesDataContext();

    var res = from positions in context.it_positions
              join vessels in context.it_vessels on positions.imo_no equals vessels.imo_no
              select;

    return res.ToList();
}


XML
Error   2   The type of one of the expressions in the join clause is incorrect.  Type inference failed in the call to 'Join'.   C:\Users\student\KM\SilverlightBingMapControl - 2013-02-11\SilverlightBingMapControl\SilverlightBingMapControl.Web\PositionService.svc.cs   40  23  SilverlightBingMapControl.Web
Error   3   The type arguments for method 'System.Linq.Enumerable.ToList<TSource>(System.Collections.Generic.IEnumerable<TSource>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.    C:\Users\student\KM\SilverlightBingMapControl - 2013-02-11\SilverlightBingMapControl\SilverlightBingMapControl.Web\PositionService.svc.cs   43  20  SilverlightBingMapControl.Web
Error   4   Invalid expression term ';' C:\Users\student\KM\SilverlightBingMapControl - 2013-02-11\SilverlightBingMapControl\SilverlightBingMapControl.Web\PositionService.svc.cs   41  29  SilverlightBingMapControl.Web
Error   5   ; expected  C:\Users\student\KM\SilverlightBingMapControl - 2013-02-11\SilverlightBingMapControl\SilverlightBingMapControl.Web\PositionService.svc.cs   41  30  SilverlightBingMapControl.Web
Posted
Updated 11-Feb-13 9:44am
v3
Comments
José Amílcar Casimiro 11-Feb-13 13:54pm    
http://www.dotnetperls.com/join

You do not mention if you are using Linq to SQL or if you are doing Entity Framework. Assuming Entity Framework:

It is interesting that you want to join two tables but then show code that is not totally relavent to the question. (I would have expected table definitions) But the general syntax would be as follows:

C#
from positions in context.ite_positions
  join vessels in context.ite_vessels on positions.imo_no equals vessels.imo_no
  select ...


Typically in this situation you are returning a self-defined object that is a culmination of the two tables. However if vessels is just a child table of positions, then an alternative is this:

C#
from positions in context.ite_positions.Include("Vessels")
where .....


In this case you are telling linq to include the child table Vessels which must match a property name on the entity. EF will handle the join and populate the child collection using the navigation property (in this case imo_no) So your entity should have a property that is defined:

C#
   public class Position
   ...
   ICollection<ite_vessels> Vessels{get;set;}
   ...
</ite_vessels>


If you google 'linq to sql join two tables' you will get more information on the syntax.
 
Share this answer
 
José Amílcar Ferreira Casimiro
: I updated the question :-)
 
Share this answer
 
Comments
José Amílcar Casimiro 11-Feb-13 16:46pm    
var res = from positions in context.it_positions
join vessels in context.it_vessels on positions.imo_no equals vessels.imo_no
select new { positions.Field1, positions.Field2, vessels.Field3, vessels.Field4 };

please make sure 'positions.imo_no' and 'vessels.imo_no' have the same datatype.
Sergey Alexandrovich Kryukov 5-May-14 0:27am    
This is not an answer. And why would you even self-accept it?
—SA

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