I have two IEnumerable
IEnumerable<MyObject> allowedObjects = MyService.GetAllowedObjects();
IEnumerable<MyOBject> preferedObjects = MyService.GetPreferedObjects();
We can safely assume that preferedObjects will always be a subset of allowedObjects.
I want to create an IDictionary<MyObject, bool>
. Objects where the key is the set of MyObjects from the allowedObjects and the bool is true if the MyObject instance was also in the preferedObjects enumerable.
I can do this by enumerating them and adding them one by one, but I would like to be able to do something like this:
IDictionary<MyObject, bool> selectedObjects = allowedObjects
.ToDictionary(o => new KeyValuePair<MyObject, bool>()
{ Key = q,
Value = preferedObjects.Any(q)
}
);
UPDATE
Exchanged Contains with Any;
The solution that is suggested most was what I tried first, but it isn't accepted for some reason:
IDictionary<MyObject, bool> selectedObjects = allowedObjects
.ToDictionary<MyObject, bool>(o => o, preferedObjects.Any(o));
Visual studio says the first method does not return a bool. Which is true, but mainly because a bool would not be the correct result to start with...
And then it says it cannot infere the type of the second lambda...
As you can see I tried to explicitly define the types to help the inference, but it doesn't solve a thing..
Suggestions?
Disclaimer: names and code are snippyfied to keep the focus where it should be