1

This Is Windows form im newbe and i have not ton of skill but i understand

i have 5 table


layer = layerID int , layerName string ( layerID = PK )

Gor = LayerID int , GroID int , GroNnumber string ( GroID = PK and LayerID = FK )

Gru = GruID int , GroID int , GruNumber string (GruID = PK and GorID = FK )

Das = GruID int , DasID int, DasNumber string (DasID = PK and GruID = FK )

Grop = GropID int , DasID int , GropNumber string (GropID = PD and DasID = FK )

all this 5 table relation ( PK - FK )

i use Join for read all data from this 5 table and my code work as well and i can see all my data from all 4 table inside my datagridview

this is my code

using (UnitOfWork db = new UnitOfWork())
{
DGVCharts.AutoGenerateColumns = false;

    var result = from layer in db.LayerRepository.Get()
        join gor in db.GorRepository.Get() on layer.LayerID equals gor.LayerID
        join gru in db.GruRepository.Get() on gor.GorID equals gru.GorID
        join das in db.DasRepository.Get() on gru.GruID equals das.GruID
        join grop in db.GropRepository.Get() on das.DastehID equals grop.DastehID


    select new
    {
        layer.LayerID,
        layer.LayerName,
        gor.GorID,
        gor.GorNumber,
        gru.GruID,
        gru.GruNumber,
        das.DasID,
        das.DasNumber,
        grop.GropID,
        grop.GropNumber,


        };
        DGVCharts.DataSource = result.ToList();

       }

     }

'''

this is my last table

Position = PositionID int, PositionCharts string, PositionName string

PositionCharts Data = ( GruNumber Data,DasNumber Data, GroNumber Data )

Data Something like this

GruNumber , DasNumber , GropNumber

Gru01, Das01, Grop01

Gru02, Das02, Grop02

FreeGru01, FreeDas01, FreeGrop01


PositionCharts : PositionName

FreeGru01: Master

FreeDas01: Member

FreeGrop01: Boss


i use this code for join this data if ( GruNumber or DasNumber or GropNumber = positioncharts ) then i see PositionName in my datagridview too

i change my code to this code

'''

using (UnitOfWork db = new UnitOfWork())
{
DGVCharts.AutoGenerateColumns = false;

    var result = from layer in db.LayerRepository.Get()
        join gor in db.GorRepository.Get() on layer.LayerID equals gor.LayerID
        join gru in db.GruRepository.Get() on gor.GorID equals gru.GorID
        join das in db.DasRepository.Get() on gru.GruID equals das.GruID
        join grop in db.GropRepository.Get() on das.DastehID equals grop.DastehID
        join position in db.PositionRepository.Get() on gru.GruNumber || das.DastehNumber || grop.GroupNumber equals position.PositionCharts

    select new
    {
        layer.LayerID,
        layer.LayerName,
        gor.GorID,
        gor.GorNumber,
        gru.GruID,
        gru.GruNumber,
        das.DasID,
        das.DasNumber,
        grop.GropID,
        grop.GropNumber,
        position.PositionID,
        position.PositionName
};
DGVCharts.DataSource = result.ToList();

}

}

''' but after i use this code i receive this error

operator '||' cannot be applied to operands of type 'string' and 'string'

i change code to this

'''

join position in db.PositionRepository.Get() on 

gru.GruNumber equals position.PositionCharts || 
das.DastehNumber equals position.PositionCharts || 
grop.GroupNumber equals position.PositionCharts

'''

not work

and this

'''

   join position in db.PositionRepository.Get() on 

    (gru.GruNumber equals position.PositionCharts || 
    das.DastehNumber equals position.PositionCharts || 
    grop.GroupNumber equals position.PositionCharts)

'''

not work !

how i can solve this problem ?

  • 3
    Well, what do you expect `string||string` would be?? – TaW Apr 23 '20 at 10:43
  • What are you trying to accomplish with this `... on gru.GruNumber || das.DastehNumber || grop.GroupNumber equals position.PositionCharts`? – 500 - Internal Server Error Apr 23 '20 at 10:48
  • @500-InternalServerError i will try read join data if ( grunumber or datehnumber or gropnumber = positioncharts ) if this 3 or 1 or 3 = positioncharts data then i can see PositionName in my datagridview ( maybe my relation table error or i most change table relation or crate new table or so ) i can give u access to check my program with ANYDESK if u like – Ali Zangeneh Apr 23 '20 at 11:10
  • @TaW yea ( all data ( GruNumber or DasNumber or GropNumber = PositionCharts ) = string ) – Ali Zangeneh Apr 23 '20 at 11:11
  • 1
    In C# you can use a List.Contains. In SQL you can use a is IN (..). – TaW Apr 23 '20 at 15:06
  • Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. Hint: LINQ only supports equijoin, you must use `from`...`where` otherwise. – NetMage Apr 23 '20 at 16:20
  • @NetMage you say i work on my code without LINQ ? and work with Normal SQL Order – Ali Zangeneh Apr 23 '20 at 18:11
  • No, but if you know how to accomplish what you want in SQL, you can translate to LINQ. – NetMage Apr 23 '20 at 18:12

1 Answers1

1

To implement a multiple condition join which is not an AND join with only equality conditions, you must use a cross join. In LINQ, this is implemented by adding another from clause and putting the join conditions in a where:

var result = from layer in db.LayerRepository.Get()
             join gor in db.GorRepository.Get() on layer.LayerID equals gor.LayerID
             join gru in db.GruRepository.Get() on gor.GorID equals gru.GorID
             join das in db.DasRepository.Get() on gru.GruID equals das.GruID
             join grop in db.GropRepository.Get() on das.DastehID equals grop.DastehID
             from position in db.PositionRepository.Get()
             where gru.GruNumber == position.PositionCharts || das.DastehNumber == position.PositionCharts || grop.GroupNumber == position.PositionCharts
             select new {
                 layer.LayerID,
                 layer.LayerName,
                 gor.GorID,
                 gor.GorNumber,
                 gru.GruID,
                 gru.GruNumber,
                 das.DasID,
                 das.DasNumber,
                 grop.GropID,
                 grop.GropNumber,
                 position.PositionID,
                 position.PositionName
             };
NetMage
  • 26,163
  • 3
  • 34
  • 55