2014年7月13日 星期日

[RESOLVED] multiplication using linq


var sum = result.AsEnumerable().Sum(x => Convert.ToDecimal(x.Odds));

If I have values in my table:

1,25

3,44



How to get 1,25*3,44=4,30 ?


Check my above code.



Hi programercek,


try following code:



var result = context.TestTables.Select(x=>x.ID).ToList();
int multiplication = result.Aggregate(1, (x, y) => x * y);

where x.ID may be replaced with x.Odds in your case. Let me know if it works for you.




Not works.



Check my code:


var result = from posts in Slip_DataTable.AsEnumerable()
where posts.Field("BettingSlipNr") == optionalSlipMenu.SelectedValue
select new
{
DateOfEvent = posts.Field("DateOfEvent"),
NameOfEvent = posts.Field("NameOfEvent"),
TypeOfBets = posts.Field("TypeOfBets"),
Pick = posts.Field("Pick"),
Odds = posts.Field("Odds"),
Stake = posts.Field("Stake"),
StakeSum = posts.Field("StakeSum"),
//Id = posts.Field("Id"),
BettingSlipNr = posts.Field("BettingSlipNr"),
};

        var result2 = result.Select(x => x.Odds).ToList();
int multiplication = result2.Aggregate(1, (x, y) => x * y);

Compilation Error


Description:
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.




Compiler Error Message: CS0019: Operator '*' cannot be applied to operands of type 'int' and 'string'



Source Error:








Check error in following line


Odds = posts.Field("Odds"),

I think you need to have int or Decimal field to have it multiplied. Convert your "Odds" field to some datatype which can be multiplied when taking them into a list and then try the same code to multiply values.



Hi ,


try somthing like thsi 


public void let()
{
// code from DevCurry.com
var arr = new[] { 5, 3, 4, 2, 6, 7 };
var sq = from int num in arr
let square = num * num
where square > 10
select new { num, square };

foreach (var a in sq)
// Console.WriteLine(a);
Response.Write(a+"
");
//Console.ReadLine();
}







Do it 


var result2 = result.Select(x => Convert.ToDecimal(x.Odds)).ToList();
int multiplication = result2.Aggregate(1, (x, y) => x * y);







I tried your code but still returns error: If I used int not works, decimal not works...


Compiler
Error Message:
CS0266: Cannot implicitly convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)



Source Error:














Line 95: 
Line 96: var result2 = result.Select(x => Convert.ToDecimal(x.Odds)).ToList();
Line 97: decimal multiplication = result2.Aggregate(1, (x, y) => x * y); Line 98:
Line 99: lblTotalOdds.Text = multiplication.ToString();



Hi ,


try Like this 


var result2 = result.Select(x => Convert.ToDecimal(x.id)).ToList();
var multiplication = result2.Aggregate(1.0, (x, y) =>Convert.ToDouble( x) * Convert.ToDouble(y));


or 


Somthing like this 


  var result2 = objEmpList.Select(x => Convert.ToDecimal(x.id)).ToList();
var multiplication = result2.Aggregate(Convert.ToDecimal(1), (x, y) => Convert.ToDecimal(x) * Convert.ToDecimal(y));







Declare "1" as decimal.. because of the first int parameter, it is handling whole function for int parameters only. check the code below.


var result = context.TestTables.Select(x=>Convert.ToDecimal(x.StrMarks)).ToList();
decimal multiplier = 1;
decimal multiplication = result.Aggregate(multiplier, (x, y) => x * y);


replace with your own column names.


沒有留言:

張貼留言