How to create a search page and function in basic web app - model-view-controller

I have a fairly simple MVC webapp connected to a database. The user needs a search page that will pull certain records from a single table. The user can give 1 or more keywords. The search function must search for records containing those keywords in 3 different columns: title, description or poc.
I've got the following setup but its incorrect. It is providing records containing any of the keywords. The results must instead be records containing all keywords. Also, I'm not sure if this is the best way of writing a search function...
// searchString contains all keywords delimited by spaces
string[] keywordArray = Regex.Split(searchString, "\\s");
var model = new List<MyTable>();
foreach (string word in keywordArray)
{
foreach (var record in myTableRepository.MyTable.Where(x => x.title.ToLower().Contains(word.ToLower()) || (x.description != null && x.description.ToLower().Contains(word.ToLower())) || (x.poc != null && x.poc.ToLower().Contains(word.ToLower()))).ToList())
{
model.Add(new MyTable
{
id = record.id,
title = record.title,
description = record.description,
poc = record.poc
});
}
}
return View(model);
For example, if the user gave the following search criteria "john test phase", then the results could look like this:
title description poc
Lorem Ipsum Test A Phase A lorem ipsum john doe
Lorem Ipsum phase This is john test for jack jane doe
Lorem Ipsum John test for jim clark phase
etc..
Thanks in advance for advice and tips!

It might be better to search one record at a time and check for all the keywords within one record. Perhaps you could collect all the words associated with a particular entry in one variable and then search that for all the keywords.
// searchString contains all keywords delimited by spaces
string[] keywordArray = Regex.Split(searchString, "\\s");
var model = new List<MyTable>();
Boolean searchResult;
foreach (var record in myTableRepository.MyTable)
{
searchResult = true;
var recordTerms = record.title + " " + record.description + " " + record.poc;
recordTerms = recordTerms.toLower();
recordArray = Regex.Split(recordTerms, "\\s");
foreach (var word in keywordArray)
{
if (!recordArray.asList.contains(word))
searchResult = false;
}
}
if (searchResult) {
model.Add(new MyTable
{
id = record.id,
title = record.title,
description = record.description,
poc = record.poc
});
}
}
return View(model);

Related

LINQ - optional/nullable where conditions

I do have the following LINQ query, selecting movies from my database that either contain the given search string or have one or more of the tags that I give to the function.
The problem is, that if the search string or the tags parameter are empty/null, I get no movies. The desired action however, is to get all the movies, so, if one parameter is null or empty, I don't want to apply it.
How can I do that?
public IEnumerable<Group<Genre, Movie>> GetMoviesGrouped(string search, List<Tag> tags)
{
var movies = from movie in Movies
where ( movie.Name.Contains(search)) && movie.Tags.Any(mt => tags.Any(t => t.ID == mt.ID))
group movie by movie.genre into g
select new Group<Genre, Movie> { Key = g.Key, Values = g };
....
}
Just for readability, I like to do it step by step
var movies = Movies;
if (!string.IsNullOrEmpty(search))
movies = movies.Where(m => m.Name.Contains(search));
if (tags != null && tags.Any()) {
var tagIds = tags.Select(m => m.ID);
movies = movies.Where(m => m.Tags.Any(x => tagIds.Contains(x.ID));
}
var result = from movie in movies
group ...
You can do something like this to skip the check if nothing is provided:
where (string.IsNullOrEmpty(search) || movie.Name.Contains(search)) &&
(!tags.Any() || movie.Tags.Any(mt => tags.Any(t => t.ID == mt.ID)))

LINQ to Entities does not recognize the method 'System.String get_Item(System.String)' method

I've looked at the various solutions here but none of them seem to work for me, probably because I'm too new to all this and am groping in the dark a bit. In the code below, the object "appointment" contains some basic LDAP information. From a list of such objects I want to be able to get a single record, based on employee id. I hope the code here is sufficient to illustrate. FTR, I've tried various formulations, including trying to use from and a select. All fail with the error given in the Title above.
IQueryable<appointment> query = null;
foreach(var record in results)
{
BoiseStateLdapDataObject record1 = record;
query = db.appointments.Where(x => x.student_id == record1.Values["employeeid"]);
}
if (query != null)
{
var selectedRecord = query.SingleOrDefault();
}
Try to move employee id getting out of query:
IQueryable<appointment> query = null;
foreach(var record in results)
{
var employeeId = record.Values["employeeid"];
query = db.appointments.Where(x => x.student_id == employeeId);
}
if (query != null)
{
var selectedRecord = query.SingleOrDefault();
}

Using an list in a query in entity framework

I am trying to find a way to pass in an optional string list to a query. What I am trying to do is filter a list of tags by the relationship between them. For example if c# was selected my program would suggest only tags that appear in documents with a c# tag and then on the selection of the next, say SQL, the tags that are linked to docs for those two tags together would be shown, whittling it down so that the user can get closer and closer to his goal.
At the moment all I have is:
List<Tag> _tags = (from t in Tags
where t.allocateTagDoc.Count > 0
select t).ToList();
This is in a method that would be called repeatedly with the optional args as tags were selected.
I think I have been coming at it arse-backwards. If I make two(or more) queries one for each supplied tag, find the docs where they all appear together and then bring out all the tags that go with them... Or would that be too many hits on the db? Can I do it entirely through an entity context variable and just query the model?
Thanks again for any help!
You can try this.
First collect tag to search in a list of strings .
List<string> tagStrings = new List<string>{"c#", "sql"};
pass this list in your query, check whether it is empty or not, if empty, it will return all the tags, else tags which matches the tagStrings.
var _tags = (from t in Tags
where t.allocateTagDoc.Count > 0
&& (tagStrings.Count ==0 || tagStrings.Contains(t.tagName))
select t).ToList();
You can also try this, Dictionary represents ID of a document with it's tags:
Dictionary<int, string[]> documents =
new Dictionary<int, string[]>();
documents.Add(1, new string[] { "C#", "SQL", "EF" });
documents.Add(2, new string[] { "C#", "Interop" });
documents.Add(3, new string[] { "Javascript", "ASP.NET" });
documents.Add(4, new string[] { });
// returns tags belonging to documents with IDs 1, 2
string[] filterTags = new string[] { "C#" };
var relatedTags = GetRelatedTags(documents, filterTags);
Debug.WriteLine(string.Join(",", relatedTags));
// returns tags belonging to document with ID 1
filterTags = new string[] { "C#", "SQL" };
relatedTags = GetRelatedTags(documents, filterTags);
Debug.WriteLine(string.Join(",", relatedTags));
// returns tags belonging to all documents
// since no filtering tags are specified
filterTags = new string[] { };
relatedTags = GetRelatedTags(documents, filterTags);
Debug.WriteLine(string.Join(",", relatedTags));
public static string[] GetRelatedTags(
Dictionary<int, string[]> documents,
string[] filterTags)
{
var documentsWithFilterTags = documents.Where(o =>
filterTags
.Intersect(o.Value).Count() == filterTags.Length);
string[] relatedTags = new string[0];
foreach (string[] tags in documentsWithFilterTags.Select(o => o.Value))
relatedTags = relatedTags
.Concat(tags)
.Distinct()
.ToArray();
return relatedTags;
}
Thought I would pop back and share my solution which was completely different to what I first had in mind.
First I altered the database a little getting rid of a useless field in the allocateDocumentTag table which enabled me to use the entity framework model much more efficiently by allowing me to leave that table out and access it purely through the relationship between Tag and Document.
When I fill my form the first time I just display all the tags that have a relationship with a document. Using my search filter after that, when a Tag is selected in a checkedListBox the Document id's that are associated with that Tag(s) are returned and are then fed back to fill the used tag listbox.
public static List<Tag> fillUsed(List<int> docIds = null)
{
List<Tag> used = new List<Tag>();
if (docIds == null || docIds.Count() < 1)
{
used = (from t in frmFocus._context.Tags
where t.Documents.Count >= 1
select t).ToList();
}
else
{
used = (from t in frmFocus._context.Tags
where t.Documents.Any(d => docIds.Contains(d.id))
select t).ToList();
}
return used;
}
From there the tags feed into the doc search and vice versa. Hope this can help someone else, if the answer is unclear or you need more code then just leave a comment and I'll try and sort it.

LINQ iterate through results

I am trying to read the following text file:
Author
{
Name xyz
blog www.test.com
rating 123
}
Author
{
Name xyz
blog www.test.com
rating 123
}
Author
{
Name xyz
blog www.test.com
rating 123
}
Author
{
Name xyz
blog www.test.com
rating 123
}
I am using the following snippet to fetch my author record:
public static IEnumerable<string> GetAuthors(string path, string startfrom, string endto)
{
return File.ReadLines(path)
.SkipWhile(line => line != startfrom)
.TakeWhile(line => line != endto);
}
public static void DoSomethingWithAuthors(string fileName)
{
var result = GetAuthors(fileName, "AUTHOR", "}").ToList();
}
The above only returns me one Author Details. Could someone kindly show me how to fetch all authors in one go so I could popluate to an object. Thank you so much!!
I rarely suggest that, but if the file structure is that predicatable you might even use regex to get your author details. As the objects you want to initialize are not complex, you can match the Author bit and take the values from regex match groups.
the regex to match the authors would be something like this:
Author\s*{\s*Name\s+(.*?)\s+blog\s+(.*?)\s+rating\s+(.*?)\s*}
Your values would be in the group 1,2 and 3.
EDIT:
If it doesn't make a difference for you, you can use the ReadToEnd() method and then you can parse the whole file content as a string:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(v=vs.100).aspx
As for the regex solution - check this out:
http://msdn.microsoft.com/en-us/library/twcw2f1c.aspx
An adapted version - it might need tweaking but in general it should work:
string text = [yourInputFileAsString]
string pat = #"Author\s*{\s*Name\s+(.*?)\s+blog\s+(.*?)\s+rating\s+(.*?)\s*}";
Regex r = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.Singleline);
Match m = r.Match(text);
var authors = new List<Author>();
while (m.Success)
{
var name = m.Groups[1].Value;
var blog = m.Groups[2].Value;
var rating = m.Groups[3].Value;
var author = new Author(name, blog, rating);
authors.Add(author);
m = m.NextMatch();
}
It is going to stop at the first } it runs into.
Remove the .TakeWhile(line => line != endto) bit and it should work for you.

Extracting text from an IEnumerable<T>

I have a IEnumerable<T> collection with Name, FullName and Address.
The Address looks like this:
Street1=54, Street2=redfer street, Pin=324234
Street1=54, Street2=fdgdgdfg street, Pin=45654
Street1=55, Street2=tryry street, Pin=65464
I want to loop through this collection and print only those Names, FullNames whose Street1=54
How can i do it in LINQ?
Ok I was able to do this to extract Street1 of the Address
coll.Address.Split(",".ToCharArray())[0]returns me Street1=54 .
Now how do I add this to the condition and print only those Name, FullName whose Street1=54
Based on your update, you can adapt Jared Par's code this way:
var result = collection.Where(x => x.Address.Contains("Street1=54"));
foreach ( var cur in result ) {
Console.WriteLine(string.Format("{0}, {1}", cur.Name, cur.FullName));
}
If you want to be able to plug in your Street1 value with a variable, then do this:
var street1 = "54";
var result = collection.Where(x => x.Address.Contains("Street1=" + street1 ));
foreach ( var cur in result ) {
Console.WriteLine(string.Format("{0}, {1}", cur.Name, cur.FullName));
}
BTW, you really should update your question or add a comment to a specific answer rather than adding a new answer that isn't.
Try this
var result = collection.Where(x => x.Address.Street1==54);
foreach ( var cur in result ) {
Console.WriteLine(var.Name);
}
Select the correct list of :
IList<T> matches = myListOfEnumerables.Where(m => m.Street1 == 54).ToList();
Then loop and print.
Actually the record looks like this:
{Name="Jan" FullName="Kathy Jan" Address="Street1=54, Street2=redfer street, Pin=324234"}
I have to loop through this collection and print only those Names, FullNames whose Street1=54
If the updated information is accurate, you should change the way you store the data.
It looks like you've packed the address information into a string. Why not store it as an object. In fact, why not just as more fields in the same object as the Name and FullName? (and why duplicate the first-name information?)
public class Person
{
public string FirstName, LastName, Street1, Street2, Pin;
}
IEnumerable<Person> persons = GetAllPersonsSomehow();
foreach (Person person in persons.Where(p => p.Street1 == "54"))
Console.WriteLine(person.LastName + ", " + person.FirstName);
Assuming you have to keep the address information in a string, you need a parser for it.
public static IDictionary<string, string> GetAddressFields(string address)
{
return address.Split(',').ToDictionary(
s => s.Substring(0, s.IndexOf('=')).Trim(),
s => s.Substring(s.IndexOf('=') + 1).Trim());
}
foreach (Person person in persons.Where(p =>
GetAddressFields(p.Address)["Street1"] == "54"))
Console.WriteLine(person.LastName + ", " + person.FirstName);
So, what you could do is that you could write a generator for parsing the Address field and then enumerating properties of that. This is a fairly common thing in the functional programming world.
To be fair you would want this code to be lazy in that it would only compute a minimal set. I'm gonna suggest some code from the BCL but you can (and probably should) rewrite the same helper methods with generators.
public static IEnumerable<KeyValuePair<string,string>> NameValueSplit( this string s )
{
foreach (var x in s.Split(','))
{
var y = x.Split(new char[] { '=' }, 2, StringSplitOptions.None);
yield return new KeyValuePair<string, string>(y[0].TrimStart(), y[1].TrimEnd());
}
}
With that helper function you can write code like this
var result = collection.Where(x => x.Address
.NameValueSplit().Any(x => x.Key == "Street1" && x.Value == "54"));
foreach ( var item in result )
{
Console.WriteLine(item.Name);
}
Now this code will not run on your SQL Server if you were thinking of that, but you could write a WHERE clause where you would search the Address field for a sub string %Street1=54%. I like lazy evaluation for string operations and think that's a lacking feature in the BCL. That is why I suggested that kind of solution.

Resources