VB.NET 到 C# Linq
VB.NET to C# Linq
我有以下 LINQ
Dim z = (From d In db.GPSdevice
Where d.CompanyId = currentuser.CompanyId And d.Type = "Truck" Or d.Type = "Trailer"
Order By d.ListOrder Descending
Group d By d.Driver Into g = Group
Select g.FirstOrDefault())
我尝试将其转换为 c#
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => new { Group = g });
但不确定,如何转换 Select g.FirstOrDefault()...
只需在 Select
中调用 g.FirstOrDefault()
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => g.FirstOrDefault());
你也可以使用query syntax in C#,无需直接使用扩展方法重写:
var z = (from d In db.GPSdevice
where (d.CompanyId == currentuser.CompanyId) && (d.Type == "Truck") || (d.Type == "Trailer")
orderby d.ListOrder descending
group d by d.Driver into g = group
select g.FirstOrDefault())
我有以下 LINQ
Dim z = (From d In db.GPSdevice
Where d.CompanyId = currentuser.CompanyId And d.Type = "Truck" Or d.Type = "Trailer"
Order By d.ListOrder Descending
Group d By d.Driver Into g = Group
Select g.FirstOrDefault())
我尝试将其转换为 c#
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => new { Group = g });
但不确定,如何转换 Select g.FirstOrDefault()...
只需在 Select
g.FirstOrDefault()
var z = db.GPSdevices
.Where(p => p.CompanyId == companyID && p.Type == "Truck" || p.Type == "Trailer")
.OrderByDescending(p => p.ListOrder)
.GroupBy(p => p.Driver)
.Select(g => g.FirstOrDefault());
你也可以使用query syntax in C#,无需直接使用扩展方法重写:
var z = (from d In db.GPSdevice
where (d.CompanyId == currentuser.CompanyId) && (d.Type == "Truck") || (d.Type == "Trailer")
orderby d.ListOrder descending
group d by d.Driver into g = group
select g.FirstOrDefault())