简体   繁体   中英

arithmatic operation in linq using entity framework mvc C#

In the method below I have to calculate a final price as per the following formula but that's giving me an error. How I can do following operation with the following query?

public JsonResult GetAllPanditBookList()
{
    var plist= db.TB_PBooking.Select(hc => new { 
        hc.PB_ID, 
        hc.PB_PRICE, 
        hc.PB_SPRICE, 
        hc.USER_ID,  
        hc.REG_DATE, 
        hc.STATUS,
        ((hc.PB_PRICE *hc.PB_SPRICE) /100) as FinalPrice,
        hc.PAYMENT_TYPE,
        hc.TB_UserReg.FULL_NAME,
        USERMOB=hc.TB_UserReg.MOBILE_NO 
    }).OrderByDescending(x => x.PB_ID).ToList();

    return Json(_templeList, JsonRequestBehavior.AllowGet);
}

Try the following:

public JsonResult GetAllPanditBookList()
{
    var plist= db.TB_PBooking.Select(hc => new 
    { 
        hc.PB_ID, 
        hc.PB_PRICE, 
        hc.PB_SPRICE, 
        hc.USER_ID,  
        hc.REG_DATE, 
        hc.STATUS,
        FinalPrice = hc.PB_PRICE * hc.PB_SPRICE /100,
        hc.PAYMENT_TYPE,
        hc.TB_UserReg.FULL_NAME,
        USERMOB = hc.TB_UserReg.MOBILE_NO 
    })
    .OrderByDescending(x => x.PB_ID)
    .ToList();

    return Json(_templeList, JsonRequestBehavior.AllowGet);
}

UPDATE:

If you need to convert prices from string to int you should change the type for these columns in your DB.

You should be able to parse string to int the following way (please note I didn't test this code):

var plist= db.TB_PBooking.Select(hc => new 
    { 
        hc.PB_ID, 
        hc.PB_PRICE, 
        hc.PB_SPRICE, 
        hc.USER_ID,  
        hc.REG_DATE, 
        hc.STATUS,
        hc.PB_PRICE,
        hc.PB_SPRICE,
        hc.PAYMENT_TYPE,
        hc.TB_UserReg.FULL_NAME,
        USERMOB=hc.TB_UserReg.MOBILE_NO             
    })
    .OrderByDescending(x => x.PB_ID)
    .AsEnumerable()
    .Select(hc => new 
    { 
        hc.PB_ID, 
        hc.PB_PRICE, 
        hc.PB_SPRICE, 
        hc.USER_ID,  
        hc.REG_DATE, 
        hc.STATUS,
        FinalPrice = (int.Parse(hc.PB_PRICE) * int.Parse(hc.PB_SPRICE)) /100,
        hc.PAYMENT_TYPE,
        hc.FULL_NAME,
        hc.USERMOB 
    })
    .ToList();

Calling .OrderByDescending(x => x.PB_ID) before AsEnumerable() lets the database order the result.

First of all, as @Thangadurai mentioned, Please take time to provide good explanation of the question which you are asking.

It is recommended to create a DTO for your result Objects and return DTO as following

      public class PanditBookDTO
      {
            int PB_ID;
            string hc.PB_PRICE;
            string hc.PB_SPRICE;
            int hc.USER_ID;
            DateTime hc.REG_DATE; 
            int hc.STATUS;
            int FinalPrice;
            hc.PAYMENT_TYPE;
            string FULL_NAME;
            string USERMOB
      }

And use this class as below :

    public JsonResult GetAllPanditBookList()
    {
        var plist= db.TB_PBooking.Select(hc => new PanditBookDTO()
        { 
            hc.PB_ID, 
            hc.PB_PRICE, 
            hc.PB_SPRICE, 
            hc.USER_ID,  
            hc.REG_DATE, 
            hc.STATUS,
            FinalPrice = (Convert.ToInt32(hc.PB_PRICE) * Convert.ToInt32(hc.PB_SPRICE)) /100,
            hc.PAYMENT_TYPE,
            hc.TB_UserReg.FULL_NAME,
            USERMOB = hc.TB_UserReg.MOBILE_NO 
        })
        .OrderByDescending(x => x.PB_ID)
        .ToList();

        return Json(_templeList, JsonRequestBehavior.AllowGet);
    }

Even in the future if you are changing your query, your response would be type safe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM