Will this skip `` inside the predicate of the .Where clause '' to mutate?

Using the Entity Framework and moving the DbContext to the class as follows:

public class Demo : IDisposable
{
  internal DbContext context;
  internal int PrivateId = 5;      

  public Demo()
  {
   this.context = new MyDbContext();
  }
  public void Question()
  {
   //Is there going to be a problem using `this` in the predicate
   var issue = this.context.SomeTable.Where(st => st.ForeignKeyId == this.PrivateId);
  }
  public Dispose()
  {
   this.context.Dispose();
  }
}

It's as simple as I could think of. I was not sure if it thiswould be somehow changed or changed when used as part of the predicate for Where. Basically I just wanted to double check because I was not sure. I checked the reimplementation ofWhere Jon Skeet, but it’s still not entirely clear what will thiskeep its value.

My ambiguity is due to the fact that predicates are transmitted as Funcwhen used with deferred execution. As a result, it would seem that the link to thiscan be used later, and not immediately after the call Where.

this?

+4
3

this ( ). this. , , this, .

, , , , - Demo. PrivateId . , PrivateId , - , .

Where, .

PrivateId , , - :

var idCopy = this.PrivateId;
var issue = this.context.SomeTable.Where(st => st.ForeignKeyId == idCopy );
+5

, this . , .

, :

var id = "12345";
var issue = this.context.SomeTable.Where(st => st.ForeignKeyId == id);

id Where? , , "12345".

this , . - Zip ForEach , this.IEnumerable - , Where Select .

+3

this. , "this", - , PrivateId.

public void Question()
{
  //Is there going to be a problem using `this` in the predicate
  var issue = context.SomeTable.Where(st => st.ForeignKeyId == PrivateId);
}
+2

Source: https://habr.com/ru/post/1525862/


All Articles