Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么Linq在没有使用过滤器的情况下只返回一个对象_C#_.net_Linq_Collections_Ienumerable - Fatal编程技术网

C# 为什么Linq在没有使用过滤器的情况下只返回一个对象

C# 为什么Linq在没有使用过滤器的情况下只返回一个对象,c#,.net,linq,collections,ienumerable,C#,.net,Linq,Collections,Ienumerable,我真的不知道如何解释这个问题。对不起,如果这是一篇重复的文章。 以下代码适用于罚款。将ID为1031的产品返回给我,当我运行过滤器(其中)时,我得到了相同的产品(因为这一个在部门和类别中)。好啊但是当我删除where 1031(第二行代码)时,它就不再工作了。名为GroupsID的IEnumerable属性在(部门的)内部只有一个值。这很奇怪。当放置where product.id==1031时,该属性有两个值(49和137),但当我删除where product.id==1031时,它只返回每

我真的不知道如何解释这个问题。对不起,如果这是一篇重复的文章。 以下代码适用于罚款。将ID为1031的产品返回给我,当我运行过滤器(其中)时,我得到了相同的产品(因为这一个在部门和类别中)。好啊但是当我删除where 1031(第二行代码)时,它就不再工作了。名为GroupsID的IEnumerable属性在(部门的)内部只有一个值。这很奇怪。当放置where product.id==1031时,该属性有两个值(49和137),但当我删除where product.id==1031时,它只返回每个产品的第一个组值(部门)。我列表中的所有产品只有一个值(大多数情况下为49)


删除
orderby Guid.NewGuid()
。一般来说,这不是一种洗牌的好方法,在某些情况下可能会破坏代码。以更好的方式执行洗牌,例如,查看或。简短的版本是,这是您可以使用的代码:

public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
    T[] elements = source.ToArray();
    for (int i = elements.Length - 1; i >= 0; i--)
    {
        // Swap element "i" with a random earlier element it (or itself)
        // ... except we don't really need to swap it fully, as we can
        // return it immediately, and afterwards it's irrelevant.
        int swapIndex = rng.Next(i + 1);
        yield return elements[swapIndex];
        elements[swapIndex] = elements[i];
    }
}
公共静态IEnumerable Shuffle(此IEnumerable源,随机rng)
{
T[]元素=source.ToArray();
对于(int i=elements.Length-1;i>=0;i--)
{
//将元素“i”与随机较早的元素it(或其本身)交换
//…除非我们真的不需要完全交换,因为我们可以
//立即归还,之后就无关紧要了。
int swapIndex=rng.Next(i+1);
收益回报要素[swapIndex];
元素[swapIndex]=元素[i];
}
}

您想用
orderby Guid.NewGuid()做什么?@TimS。在按工作方式删除订单后,很好:|感谢您所做的一切:D roflGreat,我会回答您的问题。
public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng)
{
    T[] elements = source.ToArray();
    for (int i = elements.Length - 1; i >= 0; i--)
    {
        // Swap element "i" with a random earlier element it (or itself)
        // ... except we don't really need to swap it fully, as we can
        // return it immediately, and afterwards it's irrelevant.
        int swapIndex = rng.Next(i + 1);
        yield return elements[swapIndex];
        elements[swapIndex] = elements[i];
    }
}