将 FirstOrDefault() 与 CollectionViewSource 一起使用时出现未处理的异常?

发布于 2024-12-13 01:19:02 字数 1861 浏览 1 评论 0原文

我将实体绑定到 CollectionViewSource。然后我在后面的代码中查询。因为 id 字段是 PK,所以我认为我应该使用 FirstOrDefault() 来使我的应用程序执行得更快。但它总是抛出异常...

XAML:

<Window.Resources>
        <CollectionViewSource x:Key="contractlogoesViewSource" d:DesignSource="{d:DesignInstance my:contractlogo, CreateList=True}" />
    </Window.Resources>
    <Grid>
        <Grid DataContext="{StaticResource contractlogoesViewSource}" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
...

代码隐藏:

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    VBDAdvertisement.vbdadvertisementEntities vbdadvertisementEntities = new VBDAdvertisement.vbdadvertisementEntities();            
    System.Windows.Data.CollectionViewSource contractlogoesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("contractlogoesViewSource")));
    contractlogoesViewSource.Source = vbdadvertisementEntities.contractlogoes.Where(q => q.id.Equals(3)).FirstOrDefault();
}

它抛出一个未处理的异常:

An unhandled exception of type 'System.ArgumentException' occurred in
PresentationFramework.dll
Additional information: 'VBDAdvertisement.customer' is not a valid value for
property 'Source'.

但是当我从表达式树中省略 FirstOrDefault() 时,如下所示:

contractlogoesViewSource.Source = vbdadvertisementEntities.contractlogoes.Where(q => q.id.Equals(3));

它运行良好!

那么为什么 FirstOrDefault() 会导致错误?
在这种情况下,我应该使用 FirstOrDefault() 吗?如果我使用 FirstOrDefault() ,我的应用程序执行速度会更快吗?

ps:我正在设计一个编辑表单,所以我只想加载一个项目。

I bound entities to a CollectionViewSource. Then I query in code behind. Because the id field is the PK so I think I should use FirstOrDefault() to make my app execute faster. But it always throws an exception ...

XAML :

<Window.Resources>
        <CollectionViewSource x:Key="contractlogoesViewSource" d:DesignSource="{d:DesignInstance my:contractlogo, CreateList=True}" />
    </Window.Resources>
    <Grid>
        <Grid DataContext="{StaticResource contractlogoesViewSource}" HorizontalAlignment="Left" Margin="12,12,0,0" Name="grid1" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
...

Code behind :

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    VBDAdvertisement.vbdadvertisementEntities vbdadvertisementEntities = new VBDAdvertisement.vbdadvertisementEntities();            
    System.Windows.Data.CollectionViewSource contractlogoesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("contractlogoesViewSource")));
    contractlogoesViewSource.Source = vbdadvertisementEntities.contractlogoes.Where(q => q.id.Equals(3)).FirstOrDefault();
}

It throws an An unhandled exception :

An unhandled exception of type 'System.ArgumentException' occurred in
PresentationFramework.dll
Additional information: 'VBDAdvertisement.customer' is not a valid value for
property 'Source'.

But when I omit FirstOrDefault() from the expression tree, like this :

contractlogoesViewSource.Source = vbdadvertisementEntities.contractlogoes.Where(q => q.id.Equals(3));

It works well !

So why does FirstOrDefault() cause an error ?
Is this case, should I use FirstOrDefault(), will my app execute faster if I use FirstOrDefault() ?

ps : I am designing an edit form so I just want a single item to be loaded.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

子栖 2024-12-20 01:19:02

您收到错误的原因是您无法将单个项目分配给 CollectionViewSource。如果它是编辑表单,您可能不需要 CollectionViewSource 并且可以直接绑定到该项目。

如果这样做,您可以使用 FirstorDefault() 来获取第一项。但在您的情况下,由于您通过 ID 选择,我认为最好使用 SingleOrDefault() 因为它更准确地显示您想要的意图,如下所示:

contractlogoesViewSource.Source = vbdadvertisementEntities.contractlogoes.SingleOrDefault(q => q.id.Equals(3));

SingleOrDefault()如果多个项目与谓词匹配,code> 将引发异常,但由于 ID 是主键,这对您来说不会成为问题。

如果您想使用 CollectionViewSource (但我不明白为什么),您可以使用 .Take() 来获取包含单个项目的集合,如下所示:

vbdadvertisementEntities.contractlogoes.Where(q => q.id.Equals(3)).Take(1);

The reason you're getting the error is that you can't assign a single item to a CollectionViewSource. If it's an edit form, you probably don't need a CollectionViewSource and could bind to the item directly.

If you do this you can use FirstorDefault() to get the first item. But in your case since your selecting by the ID, I think it's better to use SingleOrDefault() since it more accurately shows the intention of what you want, like so:

contractlogoesViewSource.Source = vbdadvertisementEntities.contractlogoes.SingleOrDefault(q => q.id.Equals(3));

SingleOrDefault() will throw an exception if more than one item matches the predicate, but since ID is the primary key that's not going to be a problem for you.

If you want to use a CollectionViewSource (but I can't see why), you can use .Take() to get a collection containing a single item, like so:

vbdadvertisementEntities.contractlogoes.Where(q => q.id.Equals(3)).Take(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文