0

Suppose there is a table with more than 20 columns: (col1, col2, ... )

If I want to display the sum of col1 and col2 as well as the other columns, In SQL I could:

SELECT (col1+col2), * FROM table1;

But in LINQ, I have to

from tb in table1
select new
{
  sum = tb.col1 + tb.col2,
  col1 = tb.col1,
  col2 = tb.col2,
  ...
};

Is there another simpler ways? Thanks.

Pang
  • 512
  • 3
  • 11
  • 31
  • possible duplicate of [Linq : select value in a datatable column](http://stackoverflow.com/questions/1880163/linq-select-value-in-a-datatable-column) –  Mar 28 '14 at 06:31
  • Thanks but I am not filtering the results using where. I am finding somethings like this in LINQ: select new { sum = tb.col1 + tb.col2, tb.* } – Pang Mar 28 '14 at 07:08

1 Answers1

0

You can extend type of tb entity by sum property and write something like:

table1
    .Select(tb => new 
    {
        Tb = tb,
        Sum = tb.col1 + tb.col2
    })
    .Select(x =>
    {
        x.Tb.sum = x.Sum;
        return x.Tb;
    });
Ivan Doroshenko
  • 944
  • 7
  • 13