3

I am wondering if there is a better way of accomplishing this;

I have an object - one of the property is a Dictionary. I have a set of comma separated values. I need to filter the Dictionary and get only those elements where the Dictionary value matches at least one of the values

This is what I have tried but is there is a shorter way of doing this?

Just to explain: filterValue is the list of values(E.g. "4,5')

List<string> vals = new List<string>();
vals.AddRange(filterValue.Split(new char[]{','}));
List<T> bindingEntities = entities.Where(
 e => {
 foreach(KeyValuePair<string, object> kvp in e.ExtraProperties)
 {
 if(vals.Contains(kvp.Value.ToString()))
 return true;
 }
 return false;
 }).ToList();
Yuval Itzchakov
150k32 gold badges276 silver badges333 bronze badges
asked Oct 24, 2014 at 12:57
2
  • Well, you don't need to put the results of the Split into a list and you can just do .Split(','). Commented Oct 24, 2014 at 13:09
  • Depending on how many values are in the filterValue, it might be more worthwhile to use a HashSet<T> instead of a List<T>. In fact, arrays support the Contains() method all the same, so there is no point in creating a List<T> at all either: var vals = filtervalue.Split(new char[] {','});. Commented Oct 24, 2014 at 13:10

3 Answers 3

4

You can use Any function:

List<string> vals = new List<string>();
vals.AddRange(filterValue.Split(new char[] { ',' }));
var bindingEntities = entities.Where(
 e => e.ExtraProperties.Any(
 kvp => vals.Contains(kvp.Value.ToString()))).ToList();
answered Oct 24, 2014 at 13:00
Sign up to request clarification or add additional context in comments.

5 Comments

Too Good! Short and clean!
You can even make it shorter if you drop the List<string> declaration and use the string[] returned from string.Split.
I think the parentheses around ToString() need to be fixed so that it will compile
@Slugart Nope, they're fine.
Really, Any() returns a bool and there's ToList() method on bool - just try to compile it.
0
List<string> vals = new List<string>();
vals.AddRange(filterValue.Split(new char[]{','}));
List<T> bindingEntities = entities.Where(
 e => e.ExtraProperties.Values.Any(
 val => vals.Contains(val.ToString())))
 .ToList();

or

List<string> vals = new List<string>();
vals.AddRange(filterValue.Split(new char[]{','}));
List<T> bindingEntities = from e in entities
 from val in e.ExtraProperties.Values
 where vals.Contains(val.ToString())
 select e;
answered Oct 24, 2014 at 13:02

5 Comments

ExtraProperties does not have Values property
ExtraProperties is Dictionary right? msdn.microsoft.com/en-us/library/ekcfxy3x(v=vs.110).aspx
@VanoMaisuradze am I mistaken?
I forgot that it was dictionary. So the answer is no :)
Well if you think that this downvote is mine, then take my upvote )) good luck
0

Is there a reason aren't you just querying the dictionary object?

var result = filterValue.Where(d => d.Value.Contains(val.ToString()).ToList();

Now you have the dictionary objects that contain the filter so you can work with them instead of going through the extra step of constructing a bunch of string arrays that you don't really need.

answered Oct 24, 2014 at 13:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.