I have a class I want to populate from a Linq query, but I am using a sub select statement to slightly alter the properties of the list. I have a class it should fit into but it refuses to go in. I am wondering if there is a way I can get these results to fit into the list as I defined it rather than a generic anonymous type.
public class SCADA_DATA_Truncated
{
public string acode { get; set; }
public string LOCCODE { get; set; }
public Nullable<System.DateTime> COLDATE { get; set; }
public string RESULT { get; set; }
public string analyte { get; set; }
}
And here is where I am attempting to populate the data:
List<SCADA_DATA_Truncated> dataResults = (SCADA_DATA_Truncated)(from b in a2Entity.SCADA_DATA
where DbFunctions.TruncateTime(b.COLDATE) >= dateCheck1 && DbFunctions.TruncateTime(b.COLDATE) <= dateCheck2
&& whereInAcode.Contains(b.acode) && whereInLoc.Contains(b.LOCCODE)
select new
{
COLDATE = DbFunctions.TruncateTime(b.COLDATE),
acode = b.acode,
LOCCODE = b.LOCCODE,
RESULT = b.RESULT,
analyte = b.analyte
}
).ToList();
CodePudding user response:
This is an anonymous type:
select new
{
COLDATE = DbFunctions.TruncateTime(b.COLDATE),
acode = b.acode,
LOCCODE = b.LOCCODE,
RESULT = b.RESULT,
analyte = b.analyte
}
The runtime has no idea how to convert it to your class
Why not changing it to
select new SCADA_DATA_Truncated
{
COLDATE = DbFunctions.TruncateTime(b.COLDATE),
acode = b.acode,
LOCCODE = b.LOCCODE,
RESULT = b.RESULT,
analyte = b.analyte
}
You can then remove the explicit cast altogether