Is it possible to create a column in LINQ?

1

I'm trying to turn the following query into LINQ

SELECT [nu_ano]
      ,[nu_mes]
      ,[id_projeto]
      ,[id_fase]
      ,'Financial Progress ("Competência")' as ds_categoria
      ,'Baseline' as ds_curva
       FROM [Alvarez_Marsal].[dbo].[Schedule_Status]
  where nu_mes = 12

The ds_category and ds_curve columns are created at the time the select executes because they do not exist in the table. Is it possible to do the same thing in LINQ?

So far my query in LINQ looks like this:

var result = (from l in db.Schedule_Status
.Where(x => x.nu_mes == 12)
.Select(x => new Schedule_Status
 {
   nu_ano = x.nu_ano,
   nu_mes = x.nu_mes,
   id_projeto = x.id_projeto,
   id_fase = x.id_fase
 })
select l).ToList();
    
asked by anonymous 19.04.2018 / 19:14

1 answer

1

I find this query kind of strange, but it does, would that be what you want?

var result = (from l in db.Schedule_Status
    .Where(x => x.nu_mes == 12)
    .Select(x => new Schedule_Status {
        nu_ano = x.nu_ano,
        nu_mes = x.nu_mes,
        id_projeto = x.id_projeto,
        id_fase = x.id_fase,
        ds_categoria = "Financial Progress (\"Competência\")",
        ds_curva = "Baseline"
    })
    select l).ToList();

I did not analyze if there are any errors in it, I just put what you want.

You should prepare your class to have these fields. Or use an anonymous type:

var result = (from l in db.Schedule_Status
    .Where(x => x.nu_mes == 12)
    .Select(x => new {
        nu_ano = x.nu_ano,
        nu_mes = x.nu_mes,
        id_projeto = x.id_projeto,
        id_fase = x.id_fase,
        ds_categoria = "Financial Progress (\"Competência\")",
        ds_curva = "Baseline"
    })
    select l).ToList();

I placed GitHub for future reference.

    
19.04.2018 / 19:21