C#。 NPOCO。错误:尝试加入

时间:2019-05-23 11:20:09

标签: c# sql sql-server stored-procedures npoco

我有这个存储过程,由NPOCO在下一个linq查询之后创建:

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%'

执行此存储过程后,出现错误:

  

“&”附近的语法不正确

2 个答案:

答案 0 :(得分:4)

将查询更改为重复的&&

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();

答案 1 :(得分:2)

修改Linq表达式:

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();

bool类型的属性不使用x.IsActive == truex.IsActive == false。 如果要过滤的时间是false,请使用!x.IsActive