使用linq到sql join时如何使用if语句?

时间:2014-02-22 07:40:10

标签: asp.net sql-server if-statement linq-to-sql inner-join

我正在我的asp.net项目上使用linq到sql并且我已经锁定在某个地方。我选择数据关系表,我使用以下代码。但问题是,如果supplierID不是0,则查询没有问题。查询工作成功。但如果supplierId为0,则查询不返回任何内容。我想在连接表时使用if语句但是如何使用?

我的代码,

EnvanterDataContext dc = new EnvanterDataContext();
var invent = from equipment in dc.tEquipments
             where equipment.EquipmentID == id
             orderby equipment.EquipmentID descending
join supplier in dc.tSuppliers on equipment.SupplierID equals supplier.SupplierID
join model in dc.tModels on equipment.ModelID equals model.ModelID
join equipmentType in dc.tEquipmentTypes on equipment.TypeID equals equipmentType.EquipmentTypeID
join equipmentStatus in dc.tEquipmentStatus on equipment.StatusID equals equipmentStatus.EquipmentStatusID
select equipment;

我想用

    EnvanterDataContext dc = new EnvanterDataContext();
        var invent = from equipment in dc.tEquipments
                     where equipment.EquipmentID == id
                     orderby equipment.EquipmentID descending
if(equipment.SupplierID != 0)        
join supplier in dc.tSuppliers on equipment.SupplierID equals supplier.SupplierID
        join model in dc.tModels on equipment.ModelID equals model.ModelID
        join equipmentType in dc.tEquipmentTypes on equipment.TypeID equals equipmentType.EquipmentTypeID
        join equipmentStatus in dc.tEquipmentStatus on equipment.StatusID equals equipmentStatus.EquipmentStatusID
        select equipment;

非常感谢。

0 个答案:

没有答案