Linq - SingleOrDefault and NerdDinner

I get into LinqToSql and use the NerdDinner tutorial.

I am trying to understand the syntax and would like to describe in more detail what happens on the first line that works.

Question: How to write the first request, for example, as commented code (which does not work).

public Dinner GetDinner(int id){ var result = db.Dinners.SingleOrDefault(d => d.DinnerID == id); //var result = from d in db.Dinners.SingleOrDefault // where d.DinnerID == id // select d; return (result); } 

Greetings

Dave

+4
source share
1 answer

Unfortunately, there is no declarative version of SingleOrDefault that you can use. Instead, wrap your declarative LINQ statement in parentheses as follows:

 var result = ( from d in db.Dinners where d.DinnerID == id select d ).SingleOrDefault(); 
+3
source

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


All Articles