Home > database >  Using LINQ Where with ArrayList item
Using LINQ Where with ArrayList item

Time:01-07

Someone can help me? How can I get all appsRelatedIds IN arrayList? I work on Api integration and I have the class Apps:

 public class Apps
    {
        [JsonProperty("id")]
        public string Id { get; set; }
 
        [JsonProperty("active")]
        public bool Active { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }
        
        [JsonProperty("appsRelatedIds")]
        public ArrayList RelatedId { get; set; }

   }

I've created this LINQ and works fine:

var appsFiltered = AppsList.Where(x => x.Active == true && x.Description.Contains("Top")).ToList();

But I need to get by RelatedIds like and for this response the API returns only ids related:

int relatedId = 5;  
var appsFiltered = AppsList.Where(x => x.Active == true && x.RelatedId.Contains(relatedId).ToList();

Example Data:

  1. line1 - Id 1234 Active=true Description='Top Gun' RelatedId= [ 1, 2, 3, 4, 5 ]
  2. line2 - Id 12345 Active=true Description='Top Gun' Maverick' RelatedId= [ 1, 3, 4, 5 ]
  3. line3 - Id 123456 Active=true Description='Transformers' RelatedId=[ 7,8,9]

In this case I need appsFiltered with line1 and line2 because relatedId parameter 5 is inside RelatedId ArrayList.

I tried to use a LINQ

CodePudding user response:

Why using ArrayList instead of List? ArrayList are usually slow in performance

var appsFiltered = AppsList.Where(x => x.Active == true && x.RelatedId.Any(a=> a == relatedId).ToList();

  • Related