Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6743463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:57:50+00:00 2026-05-26T11:57:50+00:00

I bound entities to a CollectionViewSource. Then I query in code behind. Because the

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:57:51+00:00Added an answer on May 26, 2026 at 11:57 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use a ComboBox which is bound to a List<> of Entities. How can
I have a WPF DataGrid bound to some entities (Entity Framework 4+). User then
I'm duty-bound by policy to use CVS in this certain project, so even though
I am building a toy app using core data for two entities Log (attributes
Bound GetChanges always returns something when bound to a UserControl 's property (even on
In a DataGridView bound to a DataView, where cells in a column will contain
I have a ComboBox bound to an ObservableCollection of decimals. What is the correct
I have a GridView bound to an ICollection<UserAnswer> that needs to show two columns:
I have an ItemsControl bound to an ObservableCollection. When the observable collection changes, I
I have the DGV bound to data and all the other controls properly. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.