0

I have a data base table with columns FirstName and Last Name and I have to make a search where the input is the full name. I'm working in c# with entities framework. I'm using the following code:

  var user = context.User.Where(i => i.FirstName + " " + i.LastName == fullName)

Is there a better or more elegant way of doing this?

I thought of doing split to the fullName but there are first names and last names made of more than one word which seems to make the search very awkward.

E.Vaughan
  • 293
  • 2
  • 13
Dov Miller
  • 1,958
  • 5
  • 34
  • 46

3 Answers3

3

You need to create an expression for it to make it work and this one you can encapsulate in the class itself or in other class, if you don't want to pollute the object itself.

You can find example at http://blog.cincura.net/230786-using-custom-properties-as-parameters-in-queries-in-ef/.

cincura.net
  • 4,130
  • 16
  • 40
0

I have a similar solution that may return more results:

var user = context.User.Where(i => i.FirstName + " " + i.LastName).Contains(fullName);

This would be similar to 'like' in SQL.

tdahman1325
  • 210
  • 3
  • 6
-1

You can create a FullName property in your business object that just returns a full name concatenating first name & last name. You can then use this property in LINQ/Lambda where necessary

public class MyUser
{
   public int UserId { get; set; }
   public string FirstName {get; set;}
   public string LastName {get; set;}
   public string FullName
   {
      get
        {
           string fullName=firstName;
           if(!string.isNullOrEmpty(LastName))
           {
               fullName= " "+ LastName;
           }
           return fullName;
        }
   } 
}
context.User.Where(i => i.FullName== fullName)
Zo Has
  • 12,599
  • 22
  • 87
  • 149