Click here to Skip to main content
15,895,606 members

Linq to SQL Join Statement in C#

Solo1233211 asked:

Open original thread
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
Tags: C#, LINQ

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900