Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
By Above code I can get details for multiple keywords but at the same time, I want to get records by single keyword also.

Please some one help me, Thanks in Advance

What I have tried:

C#
var db = new MatriModel();
string s = txtKeyWord.Text;
string[] words = s.Split(',');
int count = words.Length;

if (count <= 5))
{
    SerachByKeyWordPanel.Visible = true;
    var KeyWord = db.tblProfiles.Where(x => words.Contains(x.tblCaste.Caste) && words.Contains(x.tblCountry.Country) && words.Contains(x.City) && words.Contains(x.tblOccupation.Occupation) && words.Contains(x.tblMotherTongue.MotherTongue)).Select(x => new
  {
        ProfileID = x.ProfileID,
        ProfileFor = x.tblProfileFor.ProfileFor,
  }.ToList();
Posted
Updated 24-Nov-16 9:31am
v2
Comments
Philippe Mori 24-Nov-16 10:16am    
If you want to search specific things, then it would be better to display 5 fields on the web page and filter data as appropriate depending on which field was filled.

You have not clearly explained the problem with your code... It might not be obvious for everyone on the first reading that your code would fails to find an item as soon as one information (caste, country, city...) is missing from the words.
Member 12115746 24-Nov-16 10:59am    
Search by keywords like city,Country,Caste with single textbox is working with my above code, but with the same code if i search with single keyword ex: city, then it shows me "No record found".

Need to get records with multiple keyword search and also single keyword
Philippe Mori 24-Nov-16 11:09am    
As I said, if one information is missing, you won't find the record because you filter on all fields.

Although if the user enter a single information, it would be trivial to use || instead of && so that it could match any of the field, it won't works well if the user want to enter multiple informations but not all.

You might be able to count the number of matches but then the query would probably become too complex and it might fails or be very slow.

Thus, as I said, the best option is to have one field in your UI for each column in your database. That way, you can easily filter appropriate columns.

Well, if the data is really small (say less than 100 entries maximum), you might be able to do the filtering in memory (and not be limited by the complexity of a SQL query).

1 solution

Together with Where you have to use Any[^] Linq method ;)

Check my past answer to the question: Multiple values in 1 parameter using LINQ[^]

My best guess:
C#
var KeyWord = db.tblProfiles
    .Where(x => words.Any(y=< y==x.tblCaste.Caste) || 
                words.Any(y=< y==x.tblCountry.Country) ||
                words.Any(y=< y==x.City) ||
                words.Any(y=< y==x.tblOccupation.Occupation) ||
                words.Any(y=< y==x.tblMotherTongue.MotherTongue))
    .Select(...);


Note: not tested on large portion of data.
 
Share this answer
 
Comments
Member 12115746 25-Nov-16 10:08am    
if we have profile like India, Cast1 in one profile and in second profile India, cast2,
if we want to search india, cast2 then above code you will get 2 records. but with my requirement no profile should be displayed because user want both matching criteria profile.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900