我在使用LINQ访问C#程序中的数据时遇到了问题。我已经从访问中上传了数据。但是我们需要创建一个名为MovieFinder的应用程序,用户可以通过输入标题的一部分或导演名称的一部分来选择要观看的电影。那么有人知道该怎么做吗?只需输入部分名称即可获得所有电影?例如:输入The获取电影列表The Avengers,The Godfather,The Graduate。与导演的名字相同,输入spi,以获得斯皮尔伯格导演的所有电影。非常感谢你们。 这是我目前的代码:
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:
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); } } }推荐答案
更改此: Change this: where c.Title == title
这个:
And this:
where c.Director == director对此:
To this:
where c.Title.StartsWith(title)这个:
And this:
where c.Director.StartsWith(director)
除了 OriginalGriff [ ^ ],使用 In addition to solution1 by OriginalGriff[^], use where c.Title.Contains(title) //and/or where c.Director.Contains(director)
更多: Enumerable.Contains方法 [ ^ ]
更多推荐
C#GUI LINQ问题
发布评论