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开发者_JAVA百科 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
});
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();
Maybe something like this (as a query expression);
var lmonthlyTargetvalue = from m in lmonthlyReport
let Monthly_TargetValue = absolute ? m.Monthly_TargetValue : m.Monthly_TargetValue / 100
select new { Monthly_TargetValue , m.Month };
That might even be slight overkill for this, as it introduces an extra anonymous type behind the scenes. This is OK too;
var lmonthlyTargetvalue = lmonthlyReport.Select(m => new
{
Monthly_TargetValue = absolute ? m.Monthly_TargetValue : m.Monthly_TargetValue / 100,
m.Month
});
Maybe something like:
var lmonthlyTargetvalue =
lmonthlyReport.Select(m => new
{
IsAbsolute(m.Monthly_TargetValue) ? m.Monthly_TargetValue : m.Monthly_TargetValue / 100,
m.Month
});
Where IsAbsolute is whatever function you use to check whether the target value is a percentage or not.
This will immediately project the correct value into your variable
//If the Target value is percentual it is divided by 100
if (!Absolute)
{
lmonthlyReport.Select(m => new { m.Monthly_TargetValue, m.Month, dividedValue = m.Monthly_TargetValue/100 });
}
精彩评论