[英]Linq Results into IEnumerable in ViewModel in ASP.NET MVC Controller
我試圖將我的 linq 查詢結果放入視圖模型中,但我一直收到錯誤消息。 Linq 查詢返回多個評分結果。 我想將這些多重評級結果放在一個 IEnumerable 中。
錯誤:無法將 Models.CustomerRating 類型隱式轉換為 'System.Collections.Generic.IEnumerable<Models.CustomerRating>'。 存在顯式轉換(您是否缺少演員表?)
Linq 查詢
var data = from profile in _context.Customers
join rating in _context.CustomerRating
on profile.strUserID equals rating.strUserID into rt
from subrating in rt.DefaultIfEmpty()
where profile.strUserID == UserId
select new { p = profile, r = subrating };
我的視圖模型:
public class ServiceProvider
{
public CustomerProfile Customer { get; set; }
public IEnumerable<CustomerProfile> CustomerProfiles { get; set; }
public CustomerServices CustomerServices { get; set; }
public FilterServicesViewModel FilterServices { get; set; }
public CustomerRating Rating { get; set; }
public IEnumerable<CustomerRating> CustomerRating { get; set; }
}
我想將配置文件的結果添加到 Customer 並將子評級到 CustomerRating IEnumerable
。
我嘗試了以下操作,但出現錯誤:
foreach (var v in data)
{
serviceProviderViewModel.Customer = v.p;
serviceProviderViewModel.CustomerRating = v.r;
}
創建一個新列表並遍歷 linq 查詢結果並將其添加到該列表中
List<CustomerRating> RatingList = new List<CustomerRating>();
foreach (var v in data)
{
serviceProviderViewModel.Customer = v.p;
RatingList.Add(v.r);
}
serviceProviderViewModel.CustomerRating = RatingList;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.