I believe i’m missing something simple here, but i just can’t figure it out. Any kind of help would be much appreciated.
The problem:
The results that end up in the resultsWithMedia array only contain results that were matched against the first item in mediaTypesList. I’ve tried using Exists(), Find() and Contains(), but with no luck. I get the same kind of behavior each time.
For example: if mediaTypesList contains {"video", "link", "photo"} values, the resultsWithMedia will only hold results that are of type video.
What i’m trying to achieve is to have resultsWithMedia hold the values who’s type matches any of the values in mediaTypesList.
The code:
public void GetPostsWithMedia(string from, string before, string mediaTypes)
{
// ... some variable declarations
List<string> mediaTypesList = new List<string>();
if (!string.IsNullOrEmpty(mediaTypes))
{
var list = mediaTypes.Split(',');
foreach (var type in list)
{
mediaTypesList.Add(type);
}
}
// ... some more code
// Note: r.Data is a Dictionary that contains strings as it's values
var resultsWithMedia = (from r in response.Results
where r.Data.ContainsKey("Type") && mediaTypesList.Exists(t => t == r.Data["Type"])
select new
{
// ... different variables
}).ToArray();
}
}
You can get results with media in one query:
If you need case-insensitive comparison for media type and data, apply ToLower():
on r.Data["Type"].ToLower() equals m.ToLower(). Also consider to applym.Trim()if there is empty space in media type name after splitting string (as Euphoric suggested).