Note that there are some explanatory texts on larger screens.

plurals
  1. POViewModel QueryException using objects
    primarykey
    data
    text
    <p>Hy Guys.</p> <p>I have 2 entities: "Interactions" and "Orders", which has many "Interactions".</p> <p>On View, I wanna a different class (like <code>&lt;tr class=@cssClass&gt;</code>) for the Orders which has interactions unread. So I created a ViewModel.</p> <p>My ViewModel:</p> <pre><code>public class OrderViewModel { public Order Order{get;set;} public bool isRead{get;set;} } </code></pre> <p>My controller (I'm working with repositories):</p> <pre><code>var orders = orderRepo.All(); var viewModel = orders.Select(order=&gt; new orderViewModel { Order = order, '&lt;------------' isRead = order.Interactions.Any(x =&gt; x.Read == true), }); return View(viewModel); </code></pre> <p>If I take out the <code>Order = order,</code> It works fine. But with this code I'm getting this error:</p> <blockquote> <p>QueryException was unhandled by user code. Could not instantiate: Models.OrderViewModel</p> <p>InnerException: When casting from a number, the value must be a number less than infinity InnerException: Make sure the source type is convertible to the destination type.</p> <hr> </blockquote> <p>If, instead of use the objects I use their properties, it works fine:</p> <pre><code>ViewModel: public int IdPedido { get; set; } public string CategoryOrder { get; set; } public string ModuleOrder { get; set; } Controller: var viewModel = orders.Select( order =&gt; new OrderViewModel{ CategoryOrder=Order.Category, ModuleOrder=Order.Module, ...}); </code></pre> <p>What am I missing? I'm not understanding what should be convertible. Or isn't possible to use Objects on ViewModel, only Properties?</p> <p>If it's a usable information, I'm using the Nhibernate ORM.</p> <p>Tks guys!</p> <hr> <p><strong>EDIT</strong></p> <p>Based on @elspiko question, I realized that Order and Orders didn't have the same type. So I set orders: <code>IEnumerable&lt;Order&gt; orders = pedido.Repo.All();</code> And now it's working.</p> <p>I thought it would work before editing. Why was it considered different types?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload