2

I have the following an entity I want to search on.. how can I combine two fields to get the correct input..

something like this

var personnels = dbContext.Set<Personnel>()
                          .Where(p => 
                             (p.FirstName + ' ' + p.Surname).Contains("John Smith")
                          );

When I do that it says

Unable to create a constant value of type 'System.Object'. Only primitive types or enumeration types are supported in this context.

This is the code I am trying to fix

var personnels = dbContext.Set<Domain.Entities.App.Personnel>().Where(p =>
            ((p.GivenName  + p.Surname).Contains(criteria.PersonnelName) ||  String.IsNullOrEmpty(criteria.PersonnelName))
             && (p.PersonnelRoleId == criteria.PersonnelRoleId || (criteria.PersonnelRoleId ?? 0) == 0)
             && (((criteria.ActiveOnly && (p.ActiveFlag)) || (criteria.ActiveOnly == false)))).AsEnumerable();

The code works, but it only works if the criteria is johnsmith not john smith..

so the line is this

(p.GivenName  + p.Surname).Contains(criteria.PersonnelName) 

how can I get a space in there

(p.GivenName  + ' ' +  p.Surname).Contains(criteria.PersonnelName)  

doesn't work

user2206329
  • 2,792
  • 10
  • 54
  • 81
  • possible duplicate of [Using contains() in LINQ to SQL](http://stackoverflow.com/questions/2369022/using-contains-in-linq-to-sql) – Cyril Gandon Oct 01 '13 at 07:36

2 Answers2

2

so instead of using

(p.GivenName  + ' ' +  p.Surname).Contains(criteria.PersonnelName)  

I used

(p.GivenName  + " " +  p.Surname).Contains(criteria.PersonnelName)  

Only difference is the quotes...

and it works

user2206329
  • 2,792
  • 10
  • 54
  • 81
  • ' ' is of type char, and " " is of type string. Usually the C# compiler translates string + char + string correctly, are GivenName and Surname strings or something else? –  Oct 03 '13 at 01:32
  • Generally this type of thing isn't a problem, however, `char` is not deemed a primitive type by EF so I can see why this would fail. – James Oct 03 '13 at 08:18
0
Where(x => String.Format("{0} {1}", x.FirstName, x.LastName).Contains("John Smith")).AsEnumerable();
James
  • 80,725
  • 18
  • 167
  • 237
  • @user2206329 yeah you should be able too - not sure why you don't just use equals though? – James Oct 01 '13 at 07:55
  • for wild card searches.. if someone enters 'john'.. I want it to find anything with the word 'john' in it... I assumed by using the equals it only just finds just 'john' and none of the others that contain the word john – user2206329 Oct 01 '13 at 10:43
  • @user2206329 and in your example with `John Smith` are you looking to match on `John` & `Smith` independently or as a whole string? Not necessarily, you can use equals to check the individual strings i.e. `x.FirstName == "John" || x.LastName == "John"`. – James Oct 01 '13 at 10:52
  • i get the following error when I use the string.format {"LINQ to Entities does not recognize the method 'System.String Format(System.String, System.Object, System.Object)' method, and this method cannot be translated into a store expression."} – user2206329 Oct 03 '13 at 00:20
  • James - at the moment I have a firstname and a lastname fields. The client has an entry textbox where they can search on name. The name can be either firstname or surname or both combined. Finding users on firstname or surname is easy.. but if they enter john smith (firstname and surname) then the search on firstname or lastname fails. This is why i wanted to know how to do a combine field search – user2206329 Oct 03 '13 at 00:23
  • @user2206329 see update, if you call `AsEnumerable` it will force the `string.Format` part of the query to execute on the client-side. – James Oct 03 '13 at 08:16