c# – IQueryable给出与List不同的结果
发布时间:2020-12-15 06:18:56 所属栏目:百科 来源:网络整理
导读:如果我在我的实体框架结果上使用Select on IQueryable,结果将得到4项. 如果我使用Select on IQueryable.ToList(),我得到所有36项. 这里是函数的代码: public ImagesGetModelView Get(int start,int count){ if (count = 0) count = 9; else if (count Image
如果我在我的实体框架结果上使用Select on IQueryable,结果将得到4项.
如果我使用Select on IQueryable.ToList(),我得到所有36项. 这里是函数的代码: public ImagesGetModelView Get(int start,int count) { if (count <= 0) count = 9; else if (count > ImageHandler.MaxResult) count = ImageHandler.MaxResult; IQueryable<Image> imagesList = ImagesHandler.FetchRangeScore(start,count) .Where(m => m.Domain == Database.Enums.ImageDomain.Gfycat); //Works using list :( //var list = imagesList.ToList(); //Select all subreddits once //Returns 4 instead of 36 if not using the list ... //Returns 1 instead of 2 with Distinct() if not using the list IEnumerable<Subreddit> subreddits = imagesList .Select(m => m.Subreddit); //.Distinct(); ImagesGetModelView result = new ImagesGetModelView() { Items = imagesList,Subreddits = subreddits }; return result; } public IQueryable<Image> FetchRangeScore(int a_start,int a_count) { return Repository.AllQueryable().OrderByDescending(m => m.Score) .Skip(a_start).Take(a_count); } 在36个项目中,2个Subreddits将是不同的.但是,由于只有4个中的36个是从Select()中获取的,所以它只发现1个不同的. 那么有什么我可以用LINQ表达式来获取正确的数据,所以不同的语句是有效的,或者我必须把它变成一个列表,然后继续选择&独特的功能? 编辑: public IQueryable<Image> FetchRangeScore(int a_start,int a_count) { return Repository.AllQueryable() .Where(m => m.Domain == Database.Enums.ImageDomain.Gfycat) .OrderByDescending(m => m.Score) .Skip(a_start).Take(a_count); } 解决方法
SQL Server中的Where子句很可能与.NET不同.具体来说,根据你的排序规则设置等等,很可能各种不同的值不同,大小写或者类似的东西不同,使得它们在SQL中与Gfycat“相等”,而不是在C#中.
您可以捕获您的IQueryable<>上的.ToString()查看正在生成的SQL,并自己尝试. IQueryable<Image> imagesList = ImagesHandler.FetchRangeScore(start,count) .Where(m => m.Domain == Database.Enums.ImageDomain.Gfycat); Debug.WriteLine(imagesList.ToString()); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |