Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change values in IQueryable field in C# with LINQ

I have a variable that stores a select projection from a repository. The field in this variable are month and value. In a special case I would like to have all the value data divided by 100, WITHOUT changing the name of the variable and by keeping the information in the month field.

I know the procedure with LINQ to store permanently the items in the repository by using InsertonSubmit() but I do NOT want to store the records but just to have them modified in the context of my application. Hereby I add some code. I know it is wrong but it is just to give a better idea of what I am looking for.

Thanks in advance.

var lmonthlyTargetvalue = lmonthlyReport
                           .Select(m => new { m.Monthly_TargetValue, m.Month });

//If the Target value is percentual it is divided by 100
if (!Absolute)
{
  foreach (var monthRecord in lmonthlyTargetvalue)
  {
    var lvalue = lmonthlyTargetvalue.
             Where(m => m.Month == monthRecord.Month).
              Select(m => m.Monthly_TargetValue).First();
        lvalue = lvalue / 100;
      }
}

UPDATE:

Finally I have been able to do it thanks to your contributions. Here I list the solution:

    var lmonthlyTargetvalue = lmonthlyReport.Select(m => new
{
    Monthly_TargetValue = absolute ? m.Monthly_TargetValue : m.Monthly_TargetValue  / 100,
    m.Month
});
like image 916
CiccioMiami Avatar asked Dec 02 '25 16:12

CiccioMiami


1 Answers

this should work;

IQueryable<lmonthlyReport> lmonthlyTargetvalue = lmonthlyReport
                       .Select(m => new { m.Monthly_TargetValue, m.Month });

IList<lmonthlyReport> _temp = new List<lmonthlyReport>();

foreach(var item in lmonthlyTargetvalue){

  _temp.Add(new lmonthlyReport {

    Monthly_TargetValue = item.Monthly_TargetValue / 100,

    Month = item.Month

  });

}

return _temp.AsQueryable();
like image 148
tugberk Avatar answered Dec 04 '25 07:12

tugberk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!