Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I just got a problem on doing my LINQ to access data in C# programs. I already upload the data from access. But here is a requirement we need create an application named MovieFinder in which the user can select movies to view by entering part of the title or part of the director's name. So does anybody know who to do that? Getting all the movies by just enter part of the name? eg: enter "The" get the movie list "The Avengers", "The Godfather", "The Graduate". The same with director's name, by entering "spi", to get all the movies directed by "Spielberg". Thank you guys so much.
Here is my current code:
C#
private void searchButton_Click(object sender, EventArgs e)
        {
            if (titleradioButton.Checked == true)
            {
                string title = Convert.ToString(movietextBox.Text);

                this.moviesTableAdapter.Fill
                    (this.moviesDataSet.Movies);
                var moviesTitle =
                    from c in this.moviesDataSet.Movies
                    where c.Title == title
                    orderby c.Genre, c.Director ascending
                    group c by (string)c.Genre;
                foreach (var group in moviesTitle)
                {
                    movielistBox.Items.Add("Genre: " + group.Key);
                    foreach(var c in group)
                        movielistBox.Items.Add(c.Title + " directed by " + c.Director +
                            " \nReleased in" + c.ReleaseYear);
                }
            }
            if (directorradioButton.Checked == true)
            {
                string director = Convert.ToString(movietextBox.Text);

                this.moviesTableAdapter.Fill
                    (this.moviesDataSet.Movies);
                var moviesDirector =
                    from c in this.moviesDataSet.Movies
                    where c.Director == director
                    orderby c.Genre, c.ReleaseYear ascending
                    group c by (string)c.Genre;
                foreach (var group in moviesDirector)
                {
                    movielistBox.Items.Add("Genre: " + group.Key);
                    foreach (var c in group)
                        movielistBox.Items.Add(c.Title + " directed by " + c.Director +
                            " \nReleased in" + c.ReleaseYear);
                }
            }
        }
Posted
Updated 25-Apr-15 22:21pm
v2

Change this:
C#
where c.Title == title
And this:
C#
where c.Director == director

To this:
C#
where c.Title.StartsWith(title)
And this:
C#
where c.Director.StartsWith(director)
 
Share this answer
 
In addition to solution1 by OriginalGriff[^], use

C#
where c.Title.Contains(title)
//and/or
where c.Director.Contains(director)


More: Enumerable.Contains method[^]
 
Share this answer
 

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