Linq 实现sql中的not in和in条件查询

1.Sql中的 in 写法

Select ProductID, ProductName, CategoryID
From dbo.Products
Where CategoryID in (1, 2);

2.Sql中的 not in 写法

Select ProductID, ProductName, CategoryID
From dbo.Products
Where CategoryID not in (1, 2);

3.Linq中 in 的写法

public class CoalBillState
{
    public static CoalBillStateEnum[] UncompleteArray = new CoalBillStateEnum[]
    {
            CoalBillStateEnum.未完成,
            CoalBillStateEnum.装车已出库,
            CoalBillStateEnum.装车已回皮,
            CoalBillStateEnum.锁定,
            CoalBillStateEnum.卸车已称重
    };
 }
var creditCardSendingOrder = await _creditCardRepository.FirstOrDefaultAsync(x => x.VehicleCode == code && x.LicensePlateCode == plateNumber && (CoalBillState.UncompleteArray.Contains(x.CoalBillState)));

解析成Sql后为

SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnOrder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [dbo].[Products]AS [t0]
WHERE [t0].[State] IN (@p0, @p1)
posted @ 2022-12-14 09:29  悟自省  阅读(107)  评论(0编辑  收藏  举报