我是使用LINQ使用Entity框架的MVC的新手,我收到了以下错误
无法将类型为'System.Data.Entity.Infrastructure.DbQuery 1 [BuildMVC.Models.UserDetail]'的对象转换为'System.Collections.Generic.IList`1 [System.Object]'
以下是代码
//Here is sample code :
public ActionResult CreatePieChart()
{
Bitmap bitmapImage = new Bitmap(450, 300);
Graphics graphics = Graphics.FromImage(bitmapImage);
MemoryStream imageStream = new MemoryStream();
//Creating Chart Temaplate
var PieChart = new System.Windows.Forms.DataVisualization.Charting.Chart();
var PieChartArea = new System.Windows.Forms.DataVisualization.Charting.ChartArea();
var Legend = new System.Windows.Forms.DataVisualization.Charting.Legend();
Legend.Docking = System.Windows.Forms.DataVisualization.Charting.Docking.Bottom;
var ChartSeries = new System.Windows.Forms.DataVisualization.Charting.Series();
PieChart.Palette = System.Windows.Forms.DataVisualization.Charting.ChartColorPalette.None;
var PieChartTitle = new System.Windows.Forms.DataVisualization.Charting.Title();
PieChart.Location = new System.Drawing.Point(100, 10);
PieChart.Size = new System.Drawing.Size(450, 300);
PieChartArea.Name = "PieChartArea";
Legend.Name = "Legend";
PieChart.Name = "PieChart";
PieChart.Text = "PieChart";
PieChart.ChartAreas.Add(PieChartArea);
PieChart.Legends.Add(Legend);
//Initializing Chart Components
List<string> ChartXValues = new List<string>(); //X Values
List<Single> ChartYValues = new List<Single>(); //Y Values
ChartSeries.ChartArea = PieChartArea.Name;
ChartSeries.Legend = Legend.Name;
ChartSeries.Name = PieChart.Name;
PieChart.Series.Add(ChartSeries);
PieChartTitle.Name = "PieChart";
PieChartTitle.Text = "PieChart";
//Data Binding
PieChart.Series[ChartSeries.Name].Points.DataBindXY(ChartXValues, ChartYValues);
PieChart.Series[ChartSeries.Name].ChartType = SeriesChartType.Pie;
PieChart.Series[ChartSeries.Name].IsValueShownAsLabel = true;
PieChart.Series[ChartSeries.Name].Label = "#VALY{##0.0}%";
PieChart.Series[ChartSeries.Name].LabelForeColor = Color.White;
PieChart.SaveImage(imageStream, ChartImageFormat.Png);
PieChart.TextAntiAliasingQuality = TextAntiAliasingQuality.SystemDefault;
Response.ContentType = "image/png";
imageStream.WriteTo(Response.OutputStream);
graphics.Dispose();
bitmapImage.Dispose();
}
//To Display in page :
<img src="@Url.Action("ActionName", "ControllerName") />
我从第二个表中获取public interface IGenericRepository<TEntity> where TEntity : class
{
IList<dynamic> GetRecord(long Id);
void Add(TEntity entity);
void update(TEntity entity);
}
public IList<dynamic> GetRecord(long Id)
{
var _lstUser=(IList<dynamic>)(from ud in _dbContext.UserDetails
join rs in _dbContext.RolesMasters
on ud.FkRoleId equals rs.ID
select new UserDetail
{
FirstName = ud.FirstName + " " + ud.LastName,
PostalStreet=rs.RoleName,
IsDeleted=ud.IsDeleted
});
return _lstUser;
}
列,但由于我使用了rs.RoleName
类,所以我从UserDetail
获取了PostalStreet
列,不知道这是是否正确。
答案 0 :(得分:0)
public static IEnumerable<CustomerOrder> GetCustomerOrder(this IRepository<Customer> repository, string country)
{
var customers = repository.GetRepository<Customer>().Queryable();
var orders = repository.GetRepository<Order>().Queryable();
var query = from c in customers
join o in orders on new {a = c.CustomerID, b = c.Country}
equals new {a = o.CustomerID, b = country}
select new CustomerOrder
{
CustomerId = c.CustomerID,
ContactName = c.ContactName,
OrderId = o.OrderID,
OrderDate = o.OrderDate
};
return query.AsEnumerable();
}
这里我创建了一个额外的类,它将包含两个在WebGrid中使用的类的变量。