In the web application I'm part of a team of developers working on, we use Entity Framework as an ORM. When we perform SQL queries we typically chain link the IQueryable methods Entity Framework offers, beginning with a method Items(), which is our project specific model projection of the DbSets. I've been trying to write a Regex pattern that will find queries that doesn't use a Select method.
This is what our code typically looks like
Cars.Items()
  .Where(x => x.Year == "1988")
  .Select(x => new { x.Registration })
  .ToList();
Cars.Items()
  .Where(x => x.Id == 1923984)
  .Select(x => new { x.Registration })
  .FirstOrDefault;
This is the kind of query I'd like to find
Cars.Items()
  .Where(x => x.Id == 1923984)
  .FirstOrDefault;
I've tried using a negative lookahead to exclude queries that have a Select() method, but they are included, and I'm struggling to think of an alternative approach.
\.Items\(.*\)(\s*\..*\(.*\))*(?!\.Select\(.+\))(\s*\..*\(.*\))\;
I'll break down my logic
- \.Items\(.*\)All queries begin with this method
- (\s*\..*\(.*\))*Any number of chain linked IQueryable methods
- (?!\.Select\(.+\))Should exclude a- Select()method
- (\s*\..*\(.*\))\This could be a- First(),- FirstOrDefault(),- Single()or similar
- ;Ending the query
 
     
     
    