开发者

.net c sharp LINQ : return Object utilization

开发者 https://www.devze.com 2022-12-11 07:39 出处:网络
This is my LINQ statement var Query = from products in pLDataContex.Products where (products.Id == p) select new OrderProductMapping

This is my LINQ statement

var Query = from products in pLDataContex.Products
            where (products.Id == p)
            select new OrderProductMapping
            {
                OrderId = insertedOrderId,
                ProductId = p,
                ProductPrice = Convert.ToInt32(products.Price.ToString()),
                ProductQuantity = products.Quantity,
                ProductAmount = Convert.ToInt32(products.Price * products.Quantity)
            } into x
            select x;

what I try to do is get some details from Product table (Like price and Quan开发者_运维百科tity) and create a OrderProductMapping Object So that I can use that object's Value to user letter on in the Application .

But from Variable "Query" How can I retrieve values ? any Idea ?


Firstly, you can make your query simpler by removing the "into x select x" at the end. Just leave it at the select new ... part. You can also remove the brackets in the where clause.

I'd also suggest simple casts for the ProductPrice and ProductAmount - in particular, converting the product price into a string before parsing sounds like a bad idea to me. What are the types of Price and Quantity in the first place?

Finally, it's generally a good idea to make the range variable name singular, as it's only applied to one item at a time:

var query = from product in pLDataContex.Products
            where product.Id == p
            select new OrderProductMapping
            {
                OrderId = insertedOrderId,
                ProductId = p,
                ProductPrice = (int) product.Price,
                ProductQuantity = product.Quantity,
                ProductAmount = (int) (product.Price * products.Quantity)
            };

Now Query will be typed to IEnumerable<OrderProductMapping>. Are you expecting a single result? If so, use one of:

  • Single() - will throw if it doesn't get exactly one result
  • First() - will throw if it doesn't get any results, will ignore any result after the first
  • SingleOrDefault() - will throw if it gets more than one results, but return null if it doesn't get any
  • FirstOrDefault() - will return null if it gets any results, will ignore any result after the first

I would suggest a call which will make the system throw in all the situations (and only the situations) which indicate a programming error. If it's expected that the product may not be found (e.g. the user has entered the ID) then SingleOrDefault() is probably appropriate, assuming that Id is a primary key. You'd write:

OrderProductMapping mapping = query.SingleOrDefault();

Then react appropriate based on whether mapping is null or not.

EDIT: Reacting to the comment, the easiest way round this is to use an anonymous type, fetch the result of the query, and then build your desired result type from that:

var query = from product in pLDataContex.Products
            where product.Id == p
            select new 
            {
                OrderId = insertedOrderId,
                ProductId = p,
                ProductPrice = (int) product.Price,
                ProductQuantity = product.Quantity,
                ProductAmount = (int) (product.Price * product.Quantity)
            };

var anonMapping = query.SingleOrDefault();
if (anonMapping != null)
{
    OrderProductMapping mapping = new OrderProductMapping
    {
        OrderId = anonMapping.OrderId,
        ProductId = anonMapping.ProductId,
        ProductPrice = anonMapping.ProductPrice,
        ProductQuantity = anonMapping.ProductQuantity,
        ProductAmount = anonMapping.ProductAmount
    };
}

Note that this won't have general entity behaviour - if you modify the mapping, you'll have to explicitly save any changes to the database. I can't see that being an issue in this case though.

Another alternative is to use AsEnumerable to perform the projection in process:

var query = pLDataContex.Products
               .Where(product => product.Id == p)
               .AsEnumerable()
               .Select(product => new OrderProductMapping
                       {
                           OrderId = insertedOrderId,
                           ProductId = p,
                           ProductPrice = (int) product.Price,
                           ProductQuantity = product.Quantity,
                           ProductAmount = (int) (product.Price * 
                                                  product.Quantity)
                       });


You can enumerate the results of the Query like this:

foreach(OrderProductMapping result in Query)
{
    ...
}


foreach(var item in Query)
{
    Console.Writeline(item.OrderId);
}


foreach(var item in Query)
{
   ...
}

EDIT: Beaten! argh!

0

精彩评论

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