webdad3
webdad3

Reputation: 9080

populate object where it contains values from another list

In my object I have 3 properties:

    public String PlaceName{ get; set; }
    public Int32 PlaceId { get; set; }
    public Boolean IsChecked { get; set; }

I have a result with 5 rows being returns from my database (SQLPlaceALL).

These 5 results I always want to display. However, when I choose a contact, that contact could have one or more of these places in the results returned (sqlPlaceResults).

When I do this popPlace only contains the 3 items that sqlPlaceResults contains. I'd like my display to do something like this:

Upvotes: 1

Views: 62

Answers (2)

StriplingWarrior
StriplingWarrior

Reputation: 156624

Something like this should work. You want to query all the places, and set IsChecked based on whether there are any matching sqlPlaceResults.

var popPlace = 
    from placeAll in sqlPlaceResultsAll.AsEnumerable()
    select new Place
    {
        placeName = regions["NAME"].ToString(),
        PlaceId = (Int32)regions["PLACEID"],
        Ischecked = sqlPlaceResults.AsEnumerable().Any(
            places => places["PLACEID"] == placeAll["PLACEID"])
    };

Upvotes: 1

Justin Pihony
Justin Pihony

Reputation: 67115

You want to do a left join it seems.

Here is an MSDN on how to do this

And here is your code modified into something similar from the article:

var popPlace = 
    from places in sqlPlaceResults.AsEnumerable()
        join placeAll in sqlPlaceResultsAll.AsEnumerable() 
            on places["PLACEID"] equals placeAll["PLACEID"] into placeAllVals
    from sub in placeAllVals.DefaultIfEmpty()
    select new Place
    {
        PlaceName = regions["NAME"].ToString(),
        PlaceId = (Int32)regions["PLACEID"],
        Ischecked = sub != null
    };

Upvotes: 1

Related Questions