Search code examples
asp.net-mvcentity-frameworklinqlinq-to-entities

Multiply two fields in Linq


Is there any option for multiply two fields in LINQ

public class details
{
 public int qty { get; set; }
 public decimal unitprice{ get; set; }
 public decimal total{ get; set; }
}

select new details
{
 qty =x.qty ,
 unitprice= x.unitprice,
 total= x.qty*x.unitprice,
}

If not pls give any proper code


Solution

  • You probably want a Select() clause:

    someLinqQuery.Select(detailsObject => new details
    {
        qty = detailsObject.qty,
        unitprice = detailsObject.unitprice,
        total = detailsObject.qty * detailsObject.unitprice
    }
    

    But it looks like you need a get only property like this:

    public class details
    {
        public int qty { get; set; }
        public decimal unitprice { get; set; }
        public decimal total =>  qty * unitprice;
    }