Convert Iqueryable to a type

Asked

Viewed 119 times

1

I made that code

OrderItem itens = new OrderItem();
            var items = _orderService.GetItemsFromOrder(orderId);
            itens = items.Where(x => x.CurrencyCode == 23).Select(s => new OrderItem { ProductId = s.ProductId, CurrencyCode = s.CurrencyCode.GetValueOrDefault() }).ToList();

and you’re making that mistake:

Cannot implicitly Convert type System.Collections.Generic.List 'CSP.SubscriptionCenter.Core.domain.Orders.Orderitem' to 'CSP.SubscriptionCenter.Core.domain.Orders.Orderitem'

But the lambda I typed her as OrderItem, then it was like this:

items >> Iqueryable

Items >> Orderitem

In the case of Currencycode in Orderitem is a nullable, so I did it in the expression

CurrencyCode = s.CurrencyCode.GetValueOrDefault()

How do I resolve this above mentioned error?

OBS: In the error message I removed the <> symbols, because it was giving problems at quotar time and I put quote(').

  • 2

    You are trying to assign a List to an Orderitem type variable, that is the error, what is your question? If you only want an Orderitem you can’t do Tolist at the end.

1 answer

2

As Mauro said in the comment, there is a statement error of itens. Note that the return on Tolist is a list of OrderItem and not an element.

You can correct in two ways:

List<OrderItem> itens = new List<OrderItem>();
var items = _orderService.GetItemsFromOrder(orderId);
itens = items.Where(x => x.CurrencyCode == 23).Select(s => new OrderItem { ProductId = s.ProductId, CurrencyCode = s.CurrencyCode.GetValueOrDefault() }).ToList();

Or return only one element per FirstOrDefault:

OrderItem itens = new OrderItem();
var items = _orderService.GetItemsFromOrder(orderId);
itens = items.Where(x => x.CurrencyCode == 23).Select(s => new OrderItem { ProductId = s.ProductId, CurrencyCode = s.CurrencyCode.GetValueOrDefault() }).FirstOrDefault();

I would still recommend not to declare the type and use a var for the return you want:

var itens = items.Where(x => x.CurrencyCode == 23).Select(s => new OrderItem { ProductId = s.ProductId, CurrencyCode = s.CurrencyCode.GetValueOrDefault() }).ToList();

I hope I’ve helped.

  • Thiago, I did according to your suggestion declaring the var items, your last suggestion and I caught this error: The entity or complex type 'CSP.SubscriptionCenter.Data.OrderItem' cannot be constructed in a LINQ to Entities query.

  • I solved this by returning everything, without the select. Then in the controller I foreach and select what I really want. I just left Where, that way: var itens = items.Where(x => x.CurrencyCode == 23).ToList();

  • Do a basic test: Add a Tolist before Select. Should solve, but consider the way this is being done in the database. This is because the Entity is trying to run the conversion in the SQL command to the database, placing the Tolist first, you do the query and then select memory by converting to the typed object.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.