[英]Left join Linq is not working in Nopcommerce Service
我正在使用nopcommerce 3.4,其中我必須顯示所有供應商列表,並且基於它們的餐廳位置,因此要從左表(供應商)操作兩個表,我需要所有記錄,而從右表(Resturant),我只需要匹配的記錄如果記錄不匹配,則顯示0。
這樣我就創建了GetAllList()服務
public virtual IPagedList<ResturantCoordinatesModel> GetAllList(int pageIndex = 0, int pageSize = int.MaxValue)
{
var Resturant = (from r in _resturantCoordinates.Table
select r).ToList().AsEnumerable();
var vendor = _vendorService.GetAllVendors().ToList();
IList<ResturantCoordinatesModel> model = new List<ResturantCoordinatesModel>();
var query = from v in vendor
join r in Resturant on v.Id equals r.VendorId into gj
from rt in gj.DefaultIfEmpty()
orderby rt.Id
select new { rt.Id, v.Name, Latitude = (double?)rt.Latitude, Longitude = (double?)rt.Longitude };
}
但是在var查詢中不返回任何結果。它返回的對象引用未設置為對象的實例錯誤。請提出正確結果的建議
嘗試由Linq使用
foreach(var v in vendor)
{
var s = new ResturantCoordinatesModel();
var values = new NopDestek.StoreMapFinder.Domain.Resturant_Coordinates();
values = (from t in _resturantCoordinates.Table
where t.VendorId == v.Id
select t).FirstOrDefault();
s.Id = v.Id;
s.Name = v.Name;
s.Latitude = values == null? 0 : values.Latitude;
s.Longitude = values == null ? 0 : values.Longitude;
model.Add(s);
}
希望這可以幫助,
問候,維尼特
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.