Define many properties

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

I have the following:
Articles.Select(a => a.IsPublished = true);

But when I try to define two properties:
Articles.Select(a => a.IsPublished = true, a.UpdateAt = DateTime.Now);

I get an error on the second 'a':
The name 'a' does not exist in the current context

Am I doing something wrong or I can't do this with Linq?

Thanks,
Miguel
 
I think that, in that context, 'a' only exists within the first
condition (a => a.IsPublished = true). By putting a comma, maybe you
are creating a different scope. Try joining all your conditions using
operators.

Hope it helps,

Luis
 
shapper said:
I have the following:
Articles.Select(a => a.IsPublished = true);

But when I try to define two properties:
Articles.Select(a => a.IsPublished = true, a.UpdateAt = DateTime.Now);

I get an error on the second 'a':
The name 'a' does not exist in the current context

Am I doing something wrong or I can't do this with Linq?

What you want to do is to AND your conditions. The Select method does not
accept multiple conditions as separate arguments. Also, be carefull with the
comparison operator: one "equals" (=) means "assign", two equals (==) means
"is equal?".

Articles.Select(a => a.IsPublished && a.UpdateAt == DateTime.Now);
 
Articles.Select(a => a.IsPublished && a.UpdateAt == DateTime.Now);

Also note that a.UpdateAt == DateTime.Now will probably never yield true.

- Michael Starberg
 
Back
Top