如何在子查询中将 sql 转换为 linq
How can I convert sql to linq in Sub Query
select *
from Vehicletab
where LicencePlate not in (select LicencePlate
from AssignVechiletab
where IsAssign = 'true'
and CreatedDate = '2018-10-29')
and VendorID = 1153
你的内心 select 独立于前者。所以你可以这样做
var innerList = AssignVechiletab.Where(a => a.IsAssign && a.CreatedDate == "2018-10-29");
var mainList = Vehicletab.Where(v => !innerList.Contains(v.LicencePlate) && v.VendorID == 1153);
我不知道 LicencePlate 的类型所以我使用了 Contains 方法。您可以使用相关方法,例如 IndexOf,...
很遗憾,您忘了告诉我们您的询价要求!我必须从你的 SQL 陈述中猜测你真正想要什么。
在我看来,你有一个 AssignVehicleTabs
的序列。每个 AssignVehicleTab
至少有属性 LicensePlate
、IsAssign
和 CreatedDate
.
此外,您还有一个 VehicleTabs
序列,其中每个 VehicleTab
都有一个 LicensePlate
。
您可以查询,结果 AssignVehicleTabs
中的所有 LicensePlates
IsAssign
并且 CreatedDate
等于 2018-10-29:
DateTime myDate = new DateTime (2018, 10, 29);
var assignedLicensePlatesOnMyDate = myDbContext.AssignVehicleTabs
.Where(assignVehicleTabe => assignVehicleTab.IsAssign
&& assignVehicleTabe.CreatedDate == myDate)
.Select(assignVehicleTab => assignVehicleTab.LicensePlate);
换句话说:从所有AssignVehicleTabs
的序列中,只保留在myDate
上分配和创建的那些AssignVehicleTabs
。从每个剩余的 AssignVehicleTab
中只得到 LicensePlate
.
注意:结果是IQueryable<..>
:查询已创建,但尚未执行。为此不需要与数据库通信。
The use of proper identifiers in your lambda expressions makes them
easier to read. Advice: use singular nouns for the objects that
represent one collection item, use plural nouns for object that
represent collections.
现在,在 VehicleTabs
的集合中,您只需要 VehicleTabs
的牌照顺序为 assignedLicensePlatesOnMyDate
。
为此你使用 Queryable.Contains:
var result = myDbContext.VehicleTabs
.Where(vehicleTab => assignedLicensePlatesOnMyDate.Contains(vehicleTab.LicensePlate));
换句话说:从 VehicleTabs
的集合中,只保留 VehicleTabs
中 LicensePlate
位于 assignedLicensePlatesOnMyDate
.
中的那些 VehicleTabs
请注意,查询仍未执行。只有不 return IQueryable<...>
的 LINQ 函数才会执行查询。想想 ToList()
、FirstOrDefault()
、Any()
、Count()
等。在内部他们会调用 foreach
and/or 较低级别 GetEnumerator
和MoveNext
,这将执行查询。
由您决定是否要将其放入一个大的 LINQ 查询中。这不会提高处理速度。它肯定会降低可读性,从而降低可测试性和可维护性。
select *
from Vehicletab
where LicencePlate not in (select LicencePlate
from AssignVechiletab
where IsAssign = 'true'
and CreatedDate = '2018-10-29')
and VendorID = 1153
你的内心 select 独立于前者。所以你可以这样做
var innerList = AssignVechiletab.Where(a => a.IsAssign && a.CreatedDate == "2018-10-29");
var mainList = Vehicletab.Where(v => !innerList.Contains(v.LicencePlate) && v.VendorID == 1153);
我不知道 LicencePlate 的类型所以我使用了 Contains 方法。您可以使用相关方法,例如 IndexOf,...
很遗憾,您忘了告诉我们您的询价要求!我必须从你的 SQL 陈述中猜测你真正想要什么。
在我看来,你有一个 AssignVehicleTabs
的序列。每个 AssignVehicleTab
至少有属性 LicensePlate
、IsAssign
和 CreatedDate
.
此外,您还有一个 VehicleTabs
序列,其中每个 VehicleTab
都有一个 LicensePlate
。
您可以查询,结果 AssignVehicleTabs
中的所有 LicensePlates
IsAssign
并且 CreatedDate
等于 2018-10-29:
DateTime myDate = new DateTime (2018, 10, 29);
var assignedLicensePlatesOnMyDate = myDbContext.AssignVehicleTabs
.Where(assignVehicleTabe => assignVehicleTab.IsAssign
&& assignVehicleTabe.CreatedDate == myDate)
.Select(assignVehicleTab => assignVehicleTab.LicensePlate);
换句话说:从所有AssignVehicleTabs
的序列中,只保留在myDate
上分配和创建的那些AssignVehicleTabs
。从每个剩余的 AssignVehicleTab
中只得到 LicensePlate
.
注意:结果是IQueryable<..>
:查询已创建,但尚未执行。为此不需要与数据库通信。
The use of proper identifiers in your lambda expressions makes them easier to read. Advice: use singular nouns for the objects that represent one collection item, use plural nouns for object that represent collections.
现在,在 VehicleTabs
的集合中,您只需要 VehicleTabs
的牌照顺序为 assignedLicensePlatesOnMyDate
。
为此你使用 Queryable.Contains:
var result = myDbContext.VehicleTabs
.Where(vehicleTab => assignedLicensePlatesOnMyDate.Contains(vehicleTab.LicensePlate));
换句话说:从 VehicleTabs
的集合中,只保留 VehicleTabs
中 LicensePlate
位于 assignedLicensePlatesOnMyDate
.
VehicleTabs
请注意,查询仍未执行。只有不 return IQueryable<...>
的 LINQ 函数才会执行查询。想想 ToList()
、FirstOrDefault()
、Any()
、Count()
等。在内部他们会调用 foreach
and/or 较低级别 GetEnumerator
和MoveNext
,这将执行查询。
由您决定是否要将其放入一个大的 LINQ 查询中。这不会提高处理速度。它肯定会降低可读性,从而降低可测试性和可维护性。