附加信息:实体或复杂类型'selexexpresscl.data.truck'不能在LINQ to实体查询中构造。 [英] Additional information: the entity or complex type 'selexexpresscl.data.truck' cannot be constructed in a LINQ to entities query.
问题描述
我尝试使用linq查询,但是我收到以下错误:
无法在LINQ to Entities查询中构造实体或复杂类型'SELEXExpressCL.Data.TRUCK'。
I try query with linq but I get the following error:
The entity or complex type 'SELEXExpressCL.Data.TRUCK' cannot be constructed in a LINQ to Entities query.
TRUCK truck = (from item in context.TRUCKS
where item.NUMBER == TruckNo
select new TRUCK()
{
TRUCK_ID = item.TRUCK_ID,
NUMBER = item.NUMBER,
MAKE_TYPE = item.MAKE_TYPE,
MODEL = item.MODEL,
YEAR = item.YEAR,
VIN = item.VIN,
PLATE_NO = item.PLATE_NO,
COLOR = item.COLOR,
CODE = item.CODE,
MC_NUMBER = item.MC_NUMBER,
ANNUAL_INSPECTION = item.ANNUAL_INSPECTION,
INVESTOR_ID = item.INVESTOR_ID,
IsAVAILABLE = item.IsAVAILABLE,
STATUS_ID = item.STATUS_ID
}).Single();
< b>我尝试了什么:
我不想使用语句 forech :
What I have tried:
I don't want to use the statement forech:
var query = from a in context.TRUCKS
where a.NUMBER == TruckNo
select a;
TRUCK TruckObj = new TRUCK();
foreach (var item in query)
{
TruckObj.TRUCK_ID = item.TRUCK_ID;
TruckObj.NUMBER = item.NUMBER;
TruckObj.MAKE_TYPE = item.MAKE_TYPE;
TruckObj.MODEL = item.MODEL;
TruckObj.YEAR = item.YEAR;
TruckObj.VIN = item.VIN;
TruckObj.PLATE_NO = item.PLATE_NO;
TruckObj.COLOR = item.COLOR;
TruckObj.CODE = item.CODE;
TruckObj.MC_NUMBER = item.MC_NUMBER;
TruckObj.ANNUAL_INSPECTION = item.ANNUAL_INSPECTION;
TruckObj.INVESTOR_ID = item.INVESTOR_ID;
TruckObj.IsAVAILABLE = item.IsAVAILABLE;
TruckObj.STATUS_ID = item.STATUS_ID;
}
推荐答案
您不必创建新的TRUCK类,您已经拥有了所需的对象项目所以只需选择那个
You don't have to create a new TRUCK class, you already have the object you need in "item" so just select that
TRUCK truck = (from item in context.TRUCKS
where item.NUMBER == TruckNo
select item).Single();
BTW我用google搜索错误信息,这是第一个结果
c# - 无法在LINQ to Entities查询中构建实体 - Stack Overflow [ ^ ]
它更快你在提问之前做基础研究,因为你经常可以自己找到答案。
也考虑SingleO rDefault而不是Single,以防数据出现问题。
BTW I googled the error message and this was the first result
c# - The entity cannot be constructed in a LINQ to Entities query - Stack Overflow[^]
It's much quicker for you to do basic research before asking a question as you can often find the answer yourself.
Also consider SingleOrDefault rather than Single in case there is an issue with your data.
这篇关于附加信息:实体或复杂类型'selexexpresscl.data.truck'不能在LINQ to实体查询中构造。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!