Question

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.

Was it helpful?

Solution

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;
    });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top