Is it possible to create a column in LINQ?

Asked

Viewed 88 times

1

I am 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 is executed, as they do not exist in the table. It is possible to do the same thing in LINQ?

So far my LINQ query is 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();

1 answer

1


I think this query kind of weird, but give, would 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 didn’t analyze if there were any errors in it, I just put in what you want.

You must 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 put in the Github for future reference.

  • That would be yes, but doing so returns the following error message: 'Schedule_status' does not contain a Definition for 'ds_categoria'

  • I thought I did, and how you want to put information in a place that doesn’t have it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.