3

I'm attempting to compare list of ints to a list of objects. to see if one of the IDs match the a key in each object. If it does then return true, else false.

for example:

List<int> ints = new List<int> () { 
  1, 2, 3, 4, 5};

List<someObjectType> objects = new List<someObjectType> () { 
  {1, "one"}, {6, "six"}, {45, "forty-five"} };

(x => x.objects.any(a => ints.contains(x.id)));

However I don't know how to compare a list of ints to only one property on an object, I only know how to compare whole arrays to each other.

7
  • 1
    Are you working against an anonymous type? You won't be able to access the properties without dynamic or reflection for runtime binding, or var for compile-time binding. In a List<anon>, var is not usable. Your use of an anonymous type here seems contrived. Commented Apr 30, 2015 at 15:13
  • 4
    Why are you using List<object> instead of a Dictionary, or at least a List<KeyValuePair<int, string>>? Commented Apr 30, 2015 at 15:14
  • 2
    You could consider a Dictionary<int,string> Commented Apr 30, 2015 at 15:14
  • 1
    I agree with @CallumBradbury. The List<object> should be Dictionary<int,string> or List<KeyValuePair<int,string>>. Commented Apr 30, 2015 at 15:14
  • or at least a list of Tuple<int, string>... Commented Apr 30, 2015 at 15:15

4 Answers 4

6

Are you looking for something like that?

  List<int> ints = new List<int> () { 1, 2, 3, 4, 5};

  // For better performance when "ints" is long
  HashSet<int> ids = new HashSet<int>(ints);

  List<someObjectType> objects = new List<someObjectType> () { 
    {1, "one"}, {6, "six"}, {45, "forty-five"} };

  // if any is matched?
  boolean any = objects
    .Any(item => ids.Contains(item.id));

  // All that match
  var objectsThatMatch = objects
    .Where(item => ids.Contains(item.id));
Sign up to request clarification or add additional context in comments.

3 Comments

with a little work this ended up being the solution (p => p.object != null && p.object.Any(o => ids.Contains(o.id)));
This looks to me like it would not compile -- 'one' is not a valid C# constant.
@Hogan: you're quite right it sould be "one", "six" etc. I've just copied the code from the question
2

Your pseudo-code is pretty much there (though with Where instead of Any)...

var objectsWithIds = objects.Where(o => ints.Contains(o.Id));

Which makes me suspect this isn't what you are after...

Another way is using Intersect, but you need to translate them both into an IEnumerable<> of the same type.

var intersectionOfIds = objects.Select(_ => _.Id).Intersect(ints);

But this only gets you a list of Ids that are in both lists, not the objects themselves, which you would then need to go and find again.

Comments

0

Instead of object, you could use the keyword dynamic, but an even better solution would be to use some static type (if possible).

You can use dynamic like a dictionary, the added link will provide you with some great examples.

1 Comment

sorry - fixed. Object was just a placeholder for an actual object type.
0

I was close

(x => x.objects.any(a => ints.contains(a.id)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.