I have the following method:
public static object GetBarChart(int dataValue)
{
using (var myEntities = new i96X_dataEntities())
{
var myValues = (from values in myEntities.PointValues
where values.PointID == dataValue
select new
{
values.DataTime,
values.DataValue
}).ToList();
}
if (myValues.Count() != 0)
{
return myValues;
}
return null;
}
I can only return it as an object, because I can’t work out how to define it as its type. When I debug it tells me the return type is:
{System.Collections.Generic.List<<>f__AnonymousType2< System.DateTime,double>>}
But I can’t work out how to define that.
I have defined a class:
public class BarChart
{
public DateTime Time { get; set; }
public double Value { get; set; }
}
public class BarList
{
public List<BarChart> BarL { get; set; }
}
I presume that’s correct. But if I now use BarList as my return type the compiler reads:
Cannot convert expression type ‘System.Collections.Generic.List<{DataTime:System.DateTime, DataValue:double}>’ to return type BarList’
I think the answer is just to define BarChart, then return List< BarChart>.
Thanks for any assistance.
The function returns collection of Anonymous types. The anonymous type is defined in the LINQ expression.
If you know the type of which the PointValues collection in the DB is composed of then you can return directly that type.
If you have a class called for instance Point than change Anonymous constructor in the LINQ expression to a concrete one:
In that case your method will return a list of points. IList<< Point >>