Skip to content
Advertisement

operator ‘||’ cannot be applied to operands of type ‘string’ and ‘string’ ( how i can fix this )

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 ?

Advertisement

Answer

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
             };
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement