选择最适合查询字符串的linq对象(Select the Object with linq that best fit the Query strings)

我有一种情况,我试图将两个不同的数据源链接在一起。 看下面的代码:

static void Main(string[] args) { string url = System.Net.WebUtility.UrlDecode("yql"); HttpClient cl = new HttpClient(); var response = cl.GetStringAsync(url).Result; JObject json = JObject.Parse(response); var ids = from p in json["p"] from span in p["span"] where (string)span["content"] == "01/19/2017" select (string)span["genre"]; }

这就是JSON看起来像中的代码

请注意这个JSON与你从yql中获得的有点不同,因为我缩短了它,删除了我不需要的位。 这是完整JSON的链接 - https://query.yahooapis.com/v1/public/yql?q=SELECT%20 *%20FROM%20html%20WHERE%20url%3D%22http%3A%2F%2Fthemoviedb。有机%2Fsearch%2Fmovie%3Fquery%3Dsplit%22%20%20于是%20xpath%3D '%2F%2Fdiv%5B%40class%3D%22info%22%5D' &格式= JSON&回调=

我希望能够查询JSON并选择最匹配的对象:

string year = "2017"; string genre = "Drama, Thriller, Action"; string title = "split"

要么

public class Movie{ public string year {get; set;} public string title {get; set;} public Genre genre{get; set;} }

然后

List<Movie> _movie = new List<Movie>() _movie.add(new Movie{year = 2017, title=split, genre[]=Thriller, Drama, Action})

从查询对象和来自yql的JSON ...结果应该分开01/19/2017

I have a situation where I am trying to link two different data source together. see the code below:

static void Main(string[] args) { string url = System.Net.WebUtility.UrlDecode("yql"); HttpClient cl = new HttpClient(); var response = cl.GetStringAsync(url).Result; JObject json = JObject.Parse(response); var ids = from p in json["p"] from span in p["span"] where (string)span["content"] == "01/19/2017" select (string)span["genre"]; }

this is what the JSON looks likeenter code here

please note this JSON is a little different from what you will get from yql as I shorten it removing bits I don't need. this is the link to the full JSON - https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20html%20WHERE%20url%3D%22http%3A%2F%2Fthemoviedb.org%2Fsearch%2Fmovie%3Fquery%3Dsplit%22%20%20AND%20xpath%3D'%2F%2Fdiv%5B%40class%3D%22info%22%5D'&format=json&callback=

I want to be able to query the JSON and select the object that best match this:

string year = "2017"; string genre = "Drama, Thriller, Action"; string title = "split"

or

public class Movie{ public string year {get; set;} public string title {get; set;} public Genre genre{get; set;} }

then

List<Movie> _movie = new List<Movie>() _movie.add(new Movie{year = 2017, title=split, genre[]=Thriller, Drama, Action})

from the query object and the JSON that from yql... the result should be split 01/19/2017

最满意答案

public class Movie { public string Year { get; set; } public string Title { get; set; } public string[] Genre { get; set; } } class Program { static void Main(string[] args) { string url = System.Net.WebUtility.UrlDecode("https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20html%20WHERE%20url%3D%22http%3A%2F%2Fthemoviedb.org%2Fsearch%2Fmovie%3Fquery%3Dsplit%22%20%20AND%20xpath%3D%27%2F%2Fdiv%5B%40class%3D%22info%22%5D%27&format=json&callback="); HttpClient cl = new HttpClient(); var response = cl.GetStringAsync(url).Result; JObject json = JObject.Parse(response); var movies = new List<Movie>(); foreach (var pchild in json["query"]["results"]["div"]) { // title var title = pchild["p"][0]["a"]["title"]; var titleStr = title != null ? title.Value<string>() : string.Empty; // year var releaseDate = pchild["p"][1]["span"][0]["content"]; string releaseYear = string.Empty; DateTime temp; if (releaseDate != null && DateTime.TryParse(releaseDate.Value<string>(), System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out temp)) { releaseYear = temp.Year.ToString(); } // genres var genre = pchild["p"][1]["span"][1]["content"]; var genreArr = genre != null ? genre.Value<string>() .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries) .Select(st => st.Trim()) .ToArray() : new string[0]; movies.Add( new Movie { Title = titleStr, Year = releaseYear, Genre = genreArr }); } // searching for the best match string titleFilter = "Split"; string yearFilter = "2017"; var genreFilter = new string[] { "Drama", "Thriller", "Action" }; var bestMatches = movies .OrderByDescending(m => m.Title == titleFilter) .ThenByDescending(m => m.Year == yearFilter) .ThenByDescending(m => m.Genre.Intersect(genreFilter).Count()); // the best match var bestMatch = bestMatches.First(); Console.WriteLine(bestMatch.Title); Console.WriteLine(bestMatch.Year); Console.WriteLine(string.Join(",", bestMatch.Genre)); // all the movies already ordered //foreach (var movie in bestMatches) //{ // Console.WriteLine(movie.Title); // Console.WriteLine(string.Join(",", movie.Genre)); // Console.WriteLine(movie.Year); // Console.WriteLine(); //} Console.ReadLine(); }

输入:

string titleFilter = "Split"; string yearFilter = "2017"; var genreFilter = new string[] { "Drama", "Thriller", "Action" };

最佳匹配:

Split 2017 Drama,Horror,Thriller

请注意,您可能有多部电影具有相同的匹配项。 输入:

string titleFilter = "Split"; string yearFilter = "2016"; var genreFilter = new string[] { "Drama", "Thriller", "Action" };

最佳匹配(您可以取消注释代码的最后部分以查看所有订购的电影):

Split Fantasy,Drama 2016 Split Drama 2016 public class Movie { public string Year { get; set; } public string Title { get; set; } public string[] Genre { get; set; } } class Program { static void Main(string[] args) { string url = System.Net.WebUtility.UrlDecode("https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20html%20WHERE%20url%3D%22http%3A%2F%2Fthemoviedb.org%2Fsearch%2Fmovie%3Fquery%3Dsplit%22%20%20AND%20xpath%3D%27%2F%2Fdiv%5B%40class%3D%22info%22%5D%27&format=json&callback="); HttpClient cl = new HttpClient(); var response = cl.GetStringAsync(url).Result; JObject json = JObject.Parse(response); var movies = new List<Movie>(); foreach (var pchild in json["query"]["results"]["div"]) { // title var title = pchild["p"][0]["a"]["title"]; var titleStr = title != null ? title.Value<string>() : string.Empty; // year var releaseDate = pchild["p"][1]["span"][0]["content"]; string releaseYear = string.Empty; DateTime temp; if (releaseDate != null && DateTime.TryParse(releaseDate.Value<string>(), System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out temp)) { releaseYear = temp.Year.ToString(); } // genres var genre = pchild["p"][1]["span"][1]["content"]; var genreArr = genre != null ? genre.Value<string>() .Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries) .Select(st => st.Trim()) .ToArray() : new string[0]; movies.Add( new Movie { Title = titleStr, Year = releaseYear, Genre = genreArr }); } // searching for the best match string titleFilter = "Split"; string yearFilter = "2017"; var genreFilter = new string[] { "Drama", "Thriller", "Action" }; var bestMatches = movies .OrderByDescending(m => m.Title == titleFilter) .ThenByDescending(m => m.Year == yearFilter) .ThenByDescending(m => m.Genre.Intersect(genreFilter).Count()); // the best match var bestMatch = bestMatches.First(); Console.WriteLine(bestMatch.Title); Console.WriteLine(bestMatch.Year); Console.WriteLine(string.Join(",", bestMatch.Genre)); // all the movies already ordered //foreach (var movie in bestMatches) //{ // Console.WriteLine(movie.Title); // Console.WriteLine(string.Join(",", movie.Genre)); // Console.WriteLine(movie.Year); // Console.WriteLine(); //} Console.ReadLine(); }

Input:

string titleFilter = "Split"; string yearFilter = "2017"; var genreFilter = new string[] { "Drama", "Thriller", "Action" };

Best match:

Split 2017 Drama,Horror,Thriller

Note that you may have several movies with the same matches. Input:

string titleFilter = "Split"; string yearFilter = "2016"; var genreFilter = new string[] { "Drama", "Thriller", "Action" };

Best matches (you can uncomment the last part of code to see all the movies ordered):

Split Fantasy,Drama 2016 Split Drama 2016

更多推荐