Click here to Skip to main content
Sign Up to vote bad
good
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 - 7:43
Edited 11 Feb '13 - 9:44

Comments
José Amílcar Ferreira Casimiro - 11 Feb '13 - 13:54
http://www.dotnetperls.com/join

2 solutions

José Amílcar Ferreira Casimiro
: I updated the question Smile | :)
  Permalink  
Comments
José Amílcar Ferreira Casimiro - 11 Feb '13 - 16:46
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.
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 218
1 Sergey Alexandrovich Kryukov 159
2 Santhosh G_ 155
3 Richard MacCutchan 145
4 Maciej Los 136
0 Sergey Alexandrovich Kryukov 10,264
1 OriginalGriff 7,937
2 CPallini 4,201
3 Rohan Leuva 3,522
4 Maciej Los 3,135


Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid