LINQ select all elements of all subcomponents containing the string

I use jqueryui autocomplete to help the user select an item. I'm having trouble choosing the right elements from subcomponents of objects.
Object structure (simplified)

public class TargetType
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<SubCategory> SubCategories { get; set; }

    public TargetType()
    {
        SubCategories = new HashSet<SubCategory>();
    }
}

public class SubCategory
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<SubTargetType> SubTargetTypes { get; set; }

    public SubCategory()
    {
        SubTargetTypes = new HashSet<SubTargetType>();
    }
}

I am currently doing this with nested foreach loops, but is there a better way?
Current code:

List<SubTargetResponse> result = new List<SubTargetResponse>();
foreach (SubCategory sc in myTargetType.SubCategories)
{
    foreach (SubTargetType stt in sc.SubTargetTypes)
    {
        if (stt.Name.ToLower().Contains(type.ToLower()))
        {
            result.Add(new SubTargetResponse {
                Id = stt.Id,
                CategoryId = sc.Id,
                Name = stt.Name });
        }
    }
}
+4
source share
2 answers

You can use Linqlike this

var result = myTargetType.SubCategories
                         .SelectMany(sc => sc.SubTargetTypes)
                         .Where(stt => stt.Name.ToLower().Contains(type.ToLower()))
                         .Select(stt => new SubTargetResponse {
                                        Id = stt.Id,
                                        CategoryId = sc.Id,
                                        Name = stt.Name });

The above query does not work. The following should work, but I would not recommend it as it would not be faster or readable.

var result = myTargetType.SubCategories
                         .Select(sc => new Tuple<int, IEnumerable<SubTargetType>>
                                            (sc.Id, 
                                             sc.SubTargetTypes.Where(stt => stt.Name.ToLower().Contains(type.ToLower()))))
                         .SelectMany(tpl => tpl.Item2.Select(stt => new SubTargetResponse {
                                        Id = stt.Id,
                                        CategoryId = tpl.Item1,
                                        Name = stt.Name }));
+2
source

Actually there are two different questions.

LINQ select all elements of all subcomponents containing the string

Solutions:

(A) LINQ:

var result =
    (from sc in myTargetType.SubCategories
     from stt in sc.SubTargetTypes.Where(t => t.Name.ToLower().Contains(type.ToLower()))
     select new SubTargetResponse
     {
         Id = stt.Id,
         CategoryId = sc.Id,
         Name = stt.Name
     })
    .ToList();

(B) :

var result =
    myTargetType.SubCategories.SelectMany(
        sc => sc.SubTargetTypes.Where(stt => stt.Name.ToLower().Contains(type.ToLower())),
        (sc, stt) => new SubTargetResponse
        {
             Id = stt.Id,
             CategoryId = sc.Id,
             Name = stt.Name
        })
    .ToList();

foreach, ?

, , "". LINQ . , LINQ ( , ), LINQ this - "", , , .

0

Source: https://habr.com/ru/post/1614843/


All Articles