Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ
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:
[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();
}
 
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 11-Feb-13 8:43am
Edited 11-Feb-13 10:44am
v3
Comments
José Amílcar Ferreira Casimiro at 11-Feb-13 13:54pm
   
http://www.dotnetperls.com/join
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:

   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:
 
   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:
 
   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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

José Amílcar Ferreira Casimiro
: I updated the question Smile | :)
  Permalink  
Comments
José Amílcar Ferreira Casimiro at 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 at 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)

  Print Answers RSS
0 OriginalGriff 277
1 Maciej Los 210
2 BillWoodruff 205
3 Jochen Arndt 180
4 Sergey Alexandrovich Kryukov 165
0 OriginalGriff 5,130
1 DamithSL 4,157
2 Maciej Los 3,650
3 Kornfeld Eliyahu Peter 3,460
4 Sergey Alexandrovich Kryukov 2,811


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100