开发者

Unexpected behaviour in linq query with lazy loading on ravendb

开发者 https://www.devze.com 2023-03-29 15:20 出处:网络
I had a problem with the following code. When I don\'t call ToList() on the initial RavenSession.Query<Item>() call, the PhotoPath property is null in the ItemSummaryModel object. Is this a lazy

I had a problem with the following code. When I don't call ToList() on the initial RavenSession.Query<Item>() call, the PhotoPath property is null in the ItemSummaryModel object. Is this a lazy loading issue or something else that's causing this?

The PhotoPath property was null on the initial save of this document. I then updated it in a subsequent edit.

When I query for the full item instead of selecting a new object it works as expected populating all properties.

Why did I have to force query execution with ToList()开发者_Go百科 for the new ItemSummaryModel to be populated as expected?

var fullItems = RavenSession.Query<Item>().ToList();
var items = (from i in fullItems
             where i.DateAdded >= DateTime.Now.Subtract(new TimeSpan(10,0,0,0))
             orderby i.DateAdded
             select new ItemSummaryModel()
             {
                  Id = i.Id,
                  PhotoPath = i.ListingPhotoPath,
                  MarketingInfo = i.MarketingInfoShort,
                  Name = i.Name,
                  Summary = i.Summary,
                  PriceTypeCode = i.ClearancePrice > 0 ? PriceType.Clearance : (i.SalePrice > 0 ? PriceType.Sale : PriceType.List),
                  ListSaleOrClearancePrice = i.ClearancePrice > 0 ? i.ClearancePrice : (i.SalePrice > 0 ? i.SalePrice : i.Price)
             }).Take(nbrOfItems);
return items;


RavenDB's linq provider is pretty simplistic, it can't currently handle field remapping. In other words, it can't handle it that you did this:

                     PhotoPath = i.ListingPhotoPath,

If you changed it to

                     ListingPhotoPath = i.ListingPhotoPath,

It will work. That is an issue that is scheduled to be fixed

0

精彩评论

暂无评论...
验证码 换一张
取 消