locked
Not In / Exclude clause for different entities containing the same property RRS feed

  • Question

  • I have 2 objects, ConferencePresenter and Presenter, which both have a PresenterID property.

    I want to get a list of Presenters and EXCLUDE or NOT INCLUDE those items where the ConferencePresenter.PresenterID equals the Presenter.PresenterID using dot notation.

     

    I should be able to use either EXCLUDE, CONTAINS, or WHERE.

    Here's what I have so far but it doesn't work:

     

    // Get a list of presenters for a specific conference

    ConferencePresenter[] conferencePresenters = ConferencePresenter.GetList(conferencepresenter.conference);

     

    // Set the presenter datasource to a list of presenters sorted by name

    Presenter[] presenters = Presenter.GetList();

    // Don't include those presenters that are already attached to the current conference

    presenterRepeater.DataSource = presenters.Where(x => x.presenterID != conferencePresenters.Contains(x.presenterID));

    presenterRepeater.DataSource = presenters.OrderBy(x => x.Name);

     

    The Where clause doesn't work. I also tried Exlude and Contains but that doesn't work as well.

    The OrderBy works so I thought it would be similar for a Where, Exclude, or Contains clause.

     

    What am I doing wrong? Again, I want to do it like this (dot notation) and not with a select type clause.

     

    Thanks,

     

    John

     

    Friday, January 27, 2012 9:26 PM

Answers

  • Hi John,

    I believe what you're loking for is the .Any() method. The code would be someting like this:

    presenters.Where(x => !conferencePresenters.Any(y => y.presenterID == x.presenterID))

    Note that in the code you provided you are setting the DataSource of the repeater twice, essentially cancelling out the filter you apply to the collection. You'd want to chain the Where and OrderBy methods:

    presenterRepeater.DataSource = presenters.Where(...).OrderBy(...);

    Regards,

    Tyler

    • Marked as answer by johnr2000 Thursday, February 2, 2012 10:38 PM
    Saturday, January 28, 2012 2:36 AM

All replies

  • On 1/27/2012 4:26 PM, johnr2000 wrote:
    > I have 2 objects, ConferencePresenter and Presenter, which both have a
    > PresenterID property.
    >
    > I want to get a list of Presenters and EXCLUDE or NOT INCLUDE those
    > items where the ConferencePresenter.PresenterID equals the
    > Presenter.PresenterID using dot notation.
    >
    > I should be able to use either EXCLUDE, CONTAINS, or WHERE.
    >
    > Here's what I have so far but it doesn't work:
    >
    > // Get a list of presenters for a specific conference
    >
    > ConferencePresenter[] conferencePresenters =
    > ConferencePresenter.GetList(conferencepresenter.conference);
    >
    > // Set the presenter datasource to a list of presenters sorted by name
    >
    > Presenter[] presenters = Presenter.GetList();
    >
    > // Don't include those presenters that are already attached to the
    > current conference
    >
    > presenterRepeater.DataSource = presenters.Where(x => x.presenterID !=
    > conferencePresenters.Contains(x.presenterID));
    >
    > presenterRepeater.DataSource = presenters.OrderBy(x => x.Name);
    >
    > The Where clause doesn't work. I also tried Exlude and Contains but that
    > doesn't work as well.
    >
    > The OrderBy works so I thought it would be similar for a Where, Exclude,
    > or Contains clause.
    >
    > What am I doing wrong? Again, I want to do it like this (dot notation)
    > and not with a select type clause.
    >
     
    If the ID property is a int or long, then it should just be != with no
    Contains().
     
    conferencePresenters is not even a property it looks to be a container
    for objects, and you would need to be looking at a property of an object
    in the container.
     
     
    Friday, January 27, 2012 10:30 PM
  • Yes, conferencePresenters is a container for objects and I'm trying to look at a property of each object in that container.

    That's why I tried:

    presenterRepeater.DataSource = presenters.Where(x => x.presenterID != conferencePresenters.Contains(x.presenterID)); 

    I'm trying to exclude those the conferencePresenters from the list of presenters based on the presenterID property.

     

    Friday, January 27, 2012 11:37 PM
  • Hi John,

    I believe what you're loking for is the .Any() method. The code would be someting like this:

    presenters.Where(x => !conferencePresenters.Any(y => y.presenterID == x.presenterID))

    Note that in the code you provided you are setting the DataSource of the repeater twice, essentially cancelling out the filter you apply to the collection. You'd want to chain the Where and OrderBy methods:

    presenterRepeater.DataSource = presenters.Where(...).OrderBy(...);

    Regards,

    Tyler

    • Marked as answer by johnr2000 Thursday, February 2, 2012 10:38 PM
    Saturday, January 28, 2012 2:36 AM