[英]LINQ to SQL Join issues with FirstOrDefault()
I posted a question earlier, but I've narrowed down the problems now and it's something pretty different. 我之前发过一个问题,但我现在已经把问题缩小了,这是一个非常不同的问题。 Basically, I have a rather long query which aims to select values from a database into a custom structure (which I'll show below), but I'm having issues with the joining - paticulary trying to only select the first row of a join.
基本上,我有一个相当长的查询,旨在从数据库中选择值到一个自定义结构(我将在下面显示),但我遇到加入问题 - paiculary试图只选择一个连接的第一行。 Here is my query:
这是我的查询:
IENumerable<VehicleDetails> list = (
from s in dc.Vehicles
join veh in dc.VehicleTypes
on s.VehicleType equals veh.ID into vg
from v in vg.DefaultIfEmpty()
join consignments in dc.Consignments
.FirstOrDefault(x => x.TripDate > dateFrom &&
x.TripDate < dateTo &&
x.DeliveryDepot == depot.Letter &&
(x.DeliveryStatus == 2 || x.DeliveryStatus == 3))
on new
{
Reg = s.VehicleReg,
Depot = s.VehicleDepot
} equals new
{
Reg = consignments.VehicleReg,
Depot = consignments.DeliveryDepot
} into con
from c in con.DefaultIfEmpty()
select new
{
VehicleType = (
s.VehicleType == null ? "?":
v.VehicleType1.ToString()),
TotalRate = c.Rate + c.AddCharges,
VehicleReg = (string.IsNullOrEmpty(c.VehicleReg) ?
c.Subcontractor: c.VehicleReg),
VehicleTypeName = (v.VehicleTypeDescription == null ?
"SubContractor": v.VehicleTypeDescription)
});
My struct: 我的结构:
public struct VehicleDetails
{
internal string VehicleType;
internal decimal TotalRate;
internal string VehicleReg;
internal string VehicleTypeName;
}
With the FirstOrDefault()
in the second join, I get: 使用第二个连接中的
FirstOrDefault()
,我得到:
The type of one of the expressions in the join clause is incorrect.
join子句中某个表达式的类型不正确。 Type inference failed in the call to group join.
在对组加入的调用中类型推断失败。
Without it (and replacing the FirstOrDefault
with a Where instead), I get an error about implicity converting an anonymoustype into a 'vehicledetials' type. 没有它(并用Where替换
FirstOrDefault
),我得到一个关于将匿名类型转换为'vehdetials'类型的错误。 Both errors occur on the from c in con.DefaultIfEmpty()
call. 这两个错误都发生
from c in con.DefaultIfEmpty()
调用中的from c in con.DefaultIfEmpty()
。
Any ideas would be much, much appreciated 任何想法都会非常受欢迎
FirstOrDefault() will eagerly return a single element, but what you need is a collection (IQueryable) of elements. FirstOrDefault()会急切地返回一个元素,但你需要的是一个元素集合(IQueryable)。
So 所以
dc.Consignments
.Where(x => x.TripDate > dateFrom &&
x.TripDate < dateTo &&
x.DeliveryDepot == depot.Letter &&
(x.DeliveryStatus == 2 || x.DeliveryStatus == 3))
.Take(1)
will return a deferred collection that will have only one element. 将返回一个只有一个元素的延迟集合。
Why don't you make your query more simpler!! 为什么不让你的查询更简单!!
FirstOrDefault()
at the end of the query (last method of your query). FirstOrDefault()
(查询的最后一个方法)。 Where
better instead of FirstOrDefault
since you're going to put it at the end Where
更好而不是FirstOrDefault
因为你要把它放在最后 I think your mistake here is FirstOrDefault
with join
, when u're using join you should join with a collection not a single item (single is the result of FirstOrDefault) 我认为你的错误是
FirstOrDefault
with join
,当你使用join时你应该加入一个集合而不是单个项目(single是FirstOrDefault的结果)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.