简体   繁体   中英

compare string in multiple object list

I'm new programming, please excuse me if my question isn't properly ask

so I have an API call that returns

public class ProjectMinInfo
 {
        public string ProjectId { get; set; }
        public string Name { get; set; }
        public string Company { get; set; }
        public class ProjectMinInfos
        {
            [JsonProperty("@odata.context")]
            public string OdataContext { get; set; }
            public List<ProjectMinInfo> value { get; set; }
        }
}

I have another Api call that returns multiple result of

public class ResourcesAvailable
{
    public string ResourceId { get; set; }
    public string Description { get; set; }
    public string ResourceType { get; set; }
    public int ResourceSeq { get; set; }
    public string Companies { get; set; } //note this string could contain more than 1 value i.e "1","5"
    public class ResourcesAvailables
    {
        [JsonProperty("@odata.context")]
        public string OdataContext { get; set; }
        public List<ResourcesAvailable> value { get; set; }
    }
}

I need a function that looks at all the multiple public string Companies{ get; set; } public string Companies{ get; set; } public string Companies{ get; set; } in the second API call and then outputs only the result that contains string value similar to public string Company { get; set; } public string Company { get; set; } public string Company { get; set; } in first API call.

You need to parse the string to a list and compare the strings

Lets say you store the results of the first Api call in ProjectMinInfo projectMinInfo and the results or the second Api in List<ResourcesAvailable> resourcesAvailableList now you nee to get the object

ResourcesAvailable matchingResource = resourcesAvailableList.Where(x => x.Companies.Split(',').Contains(projectMinInfo.Company)).FirstOrDefault();
   

*I'm not sure I've understood your question properly.

Well, this is what I've understood: You have a ProjectMinInfo instance that has a company. Also, you have a collection of ResourcesAvailable instances, where each of them has some companies, separated by a comma and a space ", ", like Company1, Company2, Company3 .

You want to extract that instance of ResourcesAvailable that contains the company specified by the ProjectMinInfo instance, right?

If so, this is the LINQ Query: Say project is an instance of type ProjectMinInfo , and resources - a list of ResourcesAvailable .

// If you want the first resource that matches your condition
var matchingResource = resources
   .FirstOrDefault(q => q.Companies.Split(", ").Contains(project.Company));

// If you want all of the resources that match your condition
var matchingResources = resources
   .Where(q => q.Companies.Split(", ").Contains(project.Company))
   .ToList();

If this isn't what you're looking for, feel free to add clarifications ^^


Oh, I see the question has been asked in January

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM