我可能会对这个问题有所了解,但我在这里找不到我的错误,我很乐意提供帮助。
问题是我的List<string> names
电影名称应与db.MovieModels
中的名称相同,但每次item
都与name
相同c
我的db.MovieModels
中的c
应与db.MovieModels
匹配,但未被选中。所以我的查询保持空白。
我的linq搜索有问题吗?
(item = "Pulp Fiction"
包含电影,我可以看到断点,示例"Pulp Fiction"
和电影中的电影名为public static ListModel CreateLists(string name, DateTime date, List<string> names)
{
ObservableCollection<MovieModel> contained = new ObservableCollection<MovieModel>();
// checks if the Movie Names from "names" are in the db
foreach (var item in names)
{
var query = from c in db.MovieModels
where c.Name == item
select c;
foreach (var t in query)
{
contained.Add(t);
}
}
ListModel a = new ListModel
{
ListName = name,
DateOfCreation = date.Date,
MoviesList = contained
};
return a;
}
,但仍然是空查询。)
CreateLists("Top 10 Rated - The Top Ten", new DateTime(2017, 5, 11), new List<string>
{
"The Godfather",
"Forrest Gump",
"The Shawshank Redemption",
"The Dark Knight",
"The Lord of the Rings: The Return of the King",
"Star Wars: Episode V - The Empire Strikes Back",
"Pulp Fiction",
"Titanic",
"Saving Private Ryan",
"Star Wars: Episode IV - A New Hope",
}));
编辑:好像我忘了方法的其余部分
EDIT2:看看如何使用该方法。
<MovieModel>
Edit3:
3 BP的图片。
Edit4:
我为db.MovieModels
中的每个Name
运行循环,并将它们与'列表名称'进行比较,什么都没有!
我正在使用SQLite,我经常使用MovieModels中的SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
,所以我不明白为什么它不起作用。
答案 0 :(得分:0)
为了找到匹配的MovieModel,您可以通过调用ToList
来声明以下查询并请求它执行:
var matchedMovieModels = (from movieModel in db.MovieModels
join name in names
on movieModel.Name equals name
select movieModel).ToList();
这样做只需要往返数据库一次!虽然循环遍历names
并且每个name
试图找到哪些MovieModel可以匹配,但您可以像names.Length
那样多次往返数据库。
<强>更新强>
public static ListModel CreateLists(string name, DateTime date, List<string> names)
{
var matchedMovieModels = from movieModel in db.MovieModels
join name in names
on movieModel.Name equals name
select movieModel;
return new ListModel
{
ListName = name,
DateOfCreation = date.Date,
MoviesList = new ObservableCollection<MovieModel>(matchedMovieModels)
};
}
答案 1 :(得分:0)
它似乎适用于此代码
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ObservableCollection<MovieModel> models = CreateLists("a", DateTime.Now, new List<string>() { "a", "c" });
}
public static ObservableCollection<MovieModel> CreateLists(string name, DateTime date, List<string> names)
{
MovieModel db = new MovieModel() {
MovieModels = new List<MovieModel>() {
new MovieModel() { Name ="a"},
new MovieModel() { Name ="b"},
new MovieModel() { Name ="c"}
}
};
ObservableCollection<MovieModel> contained = new ObservableCollection<MovieModel>();
// checks if the Movie Names from "names" are in the db
foreach (var item in names)
{
var query = from c in db.MovieModels
where c.Name == item
select c;
foreach (var t in query)
{
contained.Add(t);
}
}
return contained;
}
}
public class MovieModel
{
public List<MovieModel> MovieModels { get; set; }
public string Name { get; set; }
}
}