1

I have the following object:

public class RandomModel : Metadata
{
    public string Name { get; set; }
    public ContentMod[][] Content { get; set; }

    public class ContentMod
    {
        public string Subtitle { get; set; }
        public string Text { get; set; }
        public string[] List { get; set; }
    }
}

I'm trying to search and return a RandomModel that contains a ContentMod that contains a string value input in any of its variables (Subtitle, Text, and/or List).

I am trying to do the equivalent of the following:

So if I want to search the string search input in the text of Name from the RandomModel and if it does contain the search string, return the RandomModel, I would do something like the following:

var randomContent = (from item in RandomContent
                     where item.Name.ToUpper().Contains(search)
                     select item).ToList();

Now I am trying to search text within all parameters of the ContentMod.

Something like the following:

var randomContent = (from item in RandomContent
                     where item.ContentMod.???.ToUpper().Contains(search)
                     select item).ToList();

I can't seem to figure out the ??? part above. How am I to use linq to iterate a multidimensional object's contents, and if they contain the string search, return the RandomModel.

Any help is much appreciated! Al

1 Answer 1

1

How about this bit of code if you don't mind using method syntax instead of query syntax.

var randomContent = models.Where(r => r.Content.Any(x =>
{
    return x.Any(y =>
    {
        return y.Text.ToUpper().Contains(search)
            || y.Subtitle.ToUpper().Contains(search)
            || y.List.Any(z => z.ToUpper().Contains(search));
    });
})).ToList();

I usually use IndexOf to do case insensitive searches, so in that case it would look like this.

var randomContent = models.Where(r => r.Content.Any(x =>
{
    return x.Any(y =>
    {
        return y.Text.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0
        || y.Subtitle.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0
        || y.List.Any(z => z.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0);
    });
})).ToList();

You can adust the StringComparison as needed.

Edit: As pointed out by Backs in the comments, SelectMany can be used to give you this.

var randomContent = models.Select(r => r.Content.SelectMany(o => o.Where(x =>
{
    return x.Text.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0
        || x.Subtitle.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) >= 0
        || x.List.Any(y => y.IndexOf(search, StringComparison.InvariantCultureIgnoreCase) >= 0);
}))).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

r.Content.SelectMany(o => o) can simplify expression

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.