C#. NPOCO. Error: "Incorrect syntax near '&'. " when trying to join

341 Views Asked by At

I have this stored procedure, created by NPOCO after the next linq query:

var componentVarians = _repository.Get().Include(x => x.Component)
                .Where(x => x.IsActive == true &
                            x.ServingTypeId == servingType &
                            x.Component.IsActive == true &          
 x.Component.BotanicalName.ToLower().Contains(value.ToLower())).ToList();


exec sp_executesql N'SELECT [CVD].[Id] as [Id], [CVD].[Name] as [Name], [CVD].[Sku] as [Sku], [CVD].[ServingTypeId] as [ServingTypeId], [CVD].[Price] as [Price], [CVD].[IsActive] as [IsActive], [CD].[Id] as [Component__Id], [CD].[BotanicalName] as [Component__BotanicalName], [CD].[HebrewName] as [Component__HebrewName], [CD].[PinYanName] as [Component__PinYanName], [CD].[IsActive] as [Component__IsActive], [CD].[CreatedDate] as [Component__CreatedDate] FROM [ComponentVariant] [CVD]  
  LEFT JOIN [Component] [CD] ON [CVD].[ComponentId] = [CD].[Id] 
WHERE (((([CVD].[IsActive] = @0) & ([CVD].[ServingTypeId] = @1)) & ([CD].[IsActive] = @2)) & upper(lower([CD].[BotanicalName])) like @3 escape ''\'') ',N'@0 int,@1 int,@2 int,@3 nvarchar(4000)',@0=1,@1=1,@2=1,@3=N'%A%'

After executing this stored procedure, I get an error:

Incorrect syntax near '&'

2

There are 2 best solutions below

2
andyb952 On BEST ANSWER

Change your query to be double &&

var componentVarians = _repository.Get().Include(x => x.Component)
    .Where(x => x.IsActive == true &&
                x.ServingTypeId == servingType &&
                x.Component.IsActive == true && 
                x.Component.BotanicalName.ToLower().Contains(value.ToLower())).ToList();
0
asd On

Modify the Linq expresion:

var componentVarians = _repository.Get().Include(x => x.Component)
    .Where(x => x.IsActive &&
        x.ServingTypeId == servingType &&
        x.Component.IsActive && 
        x.Component.BotanicalName.ToLower().Contains(value.ToLower())).ToList();

The properties of type bool do not use x.IsActive == true or x.IsActive == false. if you want to filter when it is false evaluate with!x.IsActive