IQueryable< T>与List< T>给出的结果不同. [英] IQueryable<T> gives different result than a List<T>

查看:93
本文介绍了IQueryable< T>与List< T>给出的结果不同.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在实体框架结果上使用Select on IQueryable,结果将得到4个项目.

If I use Select on IQueryable on my entity framework result I'll get 4 items as a result.

如果我在IQueryable.ToList()上使用Select,我将获得全部36个项目.

If I use Select on an IQueryable.ToList() I get all 36 items.

此函数的代码:

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个项目中,两个Subreddits是不同的.但是,由于从Select()中提取了36个中的4个,因此仅发现1个不同.

Out of the 36 items 2 Subreddits will be distinct. But since only 4 out of 36 are fetched from Select() it only finds 1 distinct.

对于LINQ表达式,我能做些什么来获取正确的数据,以便使不同的语句起作用,还是在继续执行Select&之前将其放入List中?功能不同?

So is there anything I can do with the LINQ expressions to get correct data so the distinct statement works or do I have to make it into a List before continuing with the Select & Distinct functions?


通过将where纬度从整个查询的结尾移动到开头. 现在看来可以正常工作了. Select会返回e.t.c ...的所有36个项目,这使Distinct可以工作,因为它可以找到多个唯一值.


by moving the where satement from the end to the start of the whole query. It appears to work correctly now. Select returns all 36 items e.t.c... which in turn makes the Distinct work since it can find more than 1 unique value.

 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);
    }

推荐答案

您的Where子句在SQL Server中的行为很可能与.NET中的行为不同.具体来说,根据您的排序规则设置等,各种.Domain值可能仅因大小写不同而有所不同,从而使它们在SQL中等于" Gfycat,而在C#中不相等.

Most likely your Where clause is behaving differently in SQL Server than it would in .NET. Specifically, depending on your collation settings and such, it's likely that various .Domain values differ only by capitalization or something like that, making them "equal" to Gfycat in SQL, but not in C#.

您可以在IQueryable<>上捕获.ToString()来查看正在生成的SQL,然后自己尝试.

You can capture the .ToString() on your IQueryable<> to see what SQL is being produced and try it yourself.

IQueryable<Image> imagesList = ImagesHandler.FetchRangeScore(start, count)
   .Where(m => m.Domain == Database.Enums.ImageDomain.Gfycat);
Debug.WriteLine(imagesList.ToString());

这篇关于IQueryable&lt; T&gt;与List&lt; T&gt;给出的结果不同.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆