gsmida
gsmida

Reputation: 357

Set values with conditions LINQ

I'm selecting some data from table A with a column value and type which have relationship with table B where there is a column coef that contains (-1,0,1) When retrieving from A I want to multiply the value with coef.

Upvotes: 0

Views: 620

Answers (2)

fecub
fecub

Reputation: 944

You could use a LINQ expression to do it in one line:

dt.Rows.ForEach(x => x["value"] = (double)x["value"] * (double)x["coef"]);

or you could just add another column to the DataTable:

dt.Columns.Add("Result", typeof(decimal));
dt["result"] = "value * coef";

Upvotes: -1

Mathew Thompson
Mathew Thompson

Reputation: 56429

Something like this?

var result = from a in tableA
             join b in tableB on a.Key = b.ForeignKey
             select new 
             {
                 Value = a.value * b.coef
             };

Upvotes: 5

Related Questions