DaImTo DaImTo - 19 days ago 7
C# Question

Find object with in class using LINQ

I want to return the item that has the profile ID I send. So in order to do this I will need to loop through all of the Items -> WebProproperties -> profile. The Class structure is at the end of the question.

I would rather use LINQ than create a nested

foreach
. I have been trying to get this to work for more than an hour now. I am stuck.

My first idea was to simply use
where
. But that doesn't work because you need to have something on the other side that needs to equal.

this.Accounts.items.Where(a => a.webProperties.Where(b => b.profiles.Where(c => c.id == pSearchString)) ).FirstOrDefault();


My second idea was to try using
Exists
which I don't have much experience with:

Item test = from item in this.Accounts.items.Exists(a => a.webProperties.Exists(b => b.profiles.Exists(c => c.id == pSearchString))) select item;


This doesn't work either:


Could not find an implementation of query pattern for source type 'Bool'


public RootObject Accounts {get; set;}

public class RootObject
{
public string kind { get; set; }
public string username { get; set; }
public int totalResults { get; set; }
public int startIndex { get; set; }
public int itemsPerPage { get; set; }
public List<Item> items { get; set; }
}

public class Profile
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
public string type { get; set; }
}

public class WebProperty
{
public string kind { get; set; }
public string id { get; set; }
public string name { get; set; }
public string internalWebPropertyId { get; set; }
public string level { get; set; }
public string websiteUrl { get; set; }
public List<Profile> profiles { get; set; }
}

public class Item
{
public string id { get; set; }
public string kind { get; set; }
public string name { get; set; }
public List<WebProperty> webProperties { get; set; }
}

Answer

You can use Any() to determine existence. Also, note that many of the extension methods have overloads which take a predicate, including FirstOrDefault():

this.Accounts.items.FirstOrDefault(a => a.webProperties
      .Any(b => b.profiles
          .Any(c => c.id == pSearchString)));