C# 在 LINQ 语句中出现“枚举没有结果”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10602374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 14:27:22  来源:igfitidea点击:

getting "Enumeration yielded no results" error in LINQ statement?

c#linqentity-framework-4

提问by Tiger

I have a repeater inside a repeater. And I am binding inner repeater on parent repeater's ItemDataBoundevent with myEventDetails list(). And I am getting this myEventDetails list()inside page_load.

我在中继器里面有一个中继器。我将内部转发器绑定到父转发器的ItemDataBound事件上myEventDetails list()。我把这个myEventDetails list()放进去page_load

So When I use the following linq query I am getting

所以当我使用以下 linq 查询时,我得到

"Enumeration yielded no results"

“枚举没有结果”

and inner repeater has empty values.

并且内部中继器具有空值。

protected void parentRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

        Repeater inner = (Repeater)(e.Item.FindControl("innerRepeater"));


        var allEvents = from x in myEventDetails
                   where x.event_name == "test"
                   select new
                   {
                       EventName = x.event_name,
                       EventID = x.event_id
                   };

        inner.DataSource = allEvents;
        inner.DataBind();
    }
}

When I am debugging I am getting values in to the myEventDetails list(). So it is not empty.

当我调试时,我正在将值放入myEventDetails list(). 所以它不是空的。

Thanks

谢谢

采纳答案by ΩmegaMan

Is it because you are not matching the right text? Try using ToLower

是不是因为您没有匹配正确的文本?尝试使用 ToLower

 where x.event_name.ToLower() == "test" 

回答by Sergey Berezovskiy

It says that there is no element with event_name == "test"in your myEventDetailscollection.

它说event_name == "test"您的myEventDetails集合中没有元素 with 。

回答by the_joric

Try calling ToList():

尝试调用ToList()

inner.DataSource = allEvents.ToList();

回答by Joe

Where is myEventDetails coming from? Is it a field/property on the class?

myEventDetails 来自哪里?它是班级的字段/属性吗?

Can you assign allEvents.ToList() to another variable, and see if that helps? I've had weird issues like this before that seemed to be solved by doing that...I never did quite figure out why, but it's worth a try.

您能否将 allEvents.ToList() 分配给另一个变量,看看是否有帮助?我以前遇到过这样的奇怪问题,但似乎可以通过这样做来解决……我从来没有弄清楚原因,但值得一试。