Find an object in a class using LINQ

I want to return an item with the profile ID that I'm sending. Therefore, for this, I will need to view all the elements -> WebProproperties ->. The class structure is at the end of the question.

I'd rather use LINQ than create a nested one foreach. I try to get this to work for more than an hour. I am stuck.

My first idea was to just use where. But this does not work, because you need to have something on the other hand, which should be 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 Existswith which I don't have much experience:
Item test =  from item in this.Accounts.items.Exists(a => a.webProperties.Exists(b => b.profiles.Exists(c => c.id == pSearchString))) select item;

This also does not work:

Could not find query template implementation 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; }
    }
+4
3

Any() . , , , FirstOrDefault():

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

.Any(), . true/false , - , .

:

if (this.Accounts.Items.Any(i=>i.webProperties.Any(wp=>wp.profiles.Any(p=>p.id == MySearchId)));

: ( , ), , , , , . .Any .FirstOrDefault, . .

var result = this.Accounts.Items.FirstOrDefault(i=>i.webProperties.Any(wp=>wp.profiles.Any(p=>p.id == MySearchId)))

+1

You can use the code below.

var abc = rr.items.Where(p => p.webProperties.Any(c => c.profiles.Any(d => d.id == "1"))).FirstOrDefault();

For your reference, your class should look like this:

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; }
    private List<Item> _items=new List<Item>();

    public List<Item> items
    {
        get { return _items; }
        set { _items = value; }
    }
}
0
source

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


All Articles