What are two ways to write this statement using LINQ?

I understand that there are two different ways to write LINQ code. Can someone show me two ways for this simple block of code. What is most commonly used or considered the easiest to debug

var subTopics = _subTopicService.GetSubTopics(Id); var subTopicsSelect = (from subTopic in subTopics select new { id = subTopic.SubTopicId, name = subTopic.Name }); 
+4
source share
3 answers

Since your request consists solely of the from and select clause, all you need to do to convert it to free syntax is a .Select call.

In free syntax, it will be:

 var subTopicsSelect = subTopics.Select(x => new { id = x.SubTopicId, name = x.Name }); 

additional literature

+3
source

You specified the first method, "SQL-like syntax", the second, "Lambda syntax":

 subTopics.Select(s => new { id = s.SubTopicId, name = s.Name }); 
+2
source

This really bothers me, as I have a completely different way to choose two methods here:

  var emailsToSend = db.emailQueues.Where( e => e.sent == false ).Take(5); var emailsToSend2 = from e2 in db.emailQueues .Take(5) .Where( e => e.sent == false ) select e2; 

Both of them seem to do the same, but I prefer the syntax of the first method. It is easier to remember.

0
source

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


All Articles