1

How can I do the following sql code with linq?

SQL;

   SELECT BusinessEntityID, FirstName + ' ' + LastName AS "FullName" 
FROM Person WHERE FullName LIKE  'a%'

LINQ;

 using(var db= new db_Context)
 {
      var query = db.Person.Select(q=> q.FirstName  + " "+ q=>q.FullName)
 }
yuksel
  • 71
  • 1
  • 13

2 Answers2

0

Searching after concatenate the strings is not a good idea. I recommend the search for firstname and lastname separately

var query = db.Person.Where (t => t.FirstName.StartsWith("a") || t.LastName.StartsWith("a") )
                     .Select(q=> new { q.BusinessEntityID, Fullname = q.FirstName  + " " + q.LastName })
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44
0

Following code will be helpful to you,

var list1 =  db.Person.Where (t => t.FirstName.StartsWith("a"))
                     .Select(q=> new { 
                             BusinessEntityID = q.BusinessEntityID, 
                             Fullname = q.FirstName + " " + q.LastName 
                      });

Or

var list2 = from psn in db.Person
            where psn.FirstName.StartsWith("a")
            select new {
                  BusinessEntityID = psn.BusinessEntityID, 
                  Fullname = psn.FirstName + " " + psn.LastName 
            };
Abhilash Ravindran C K
  • 1,818
  • 2
  • 13
  • 22