简体   繁体   English

比较多个 object 列表中的字符串

[英]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所以我有一个返回的 API 调用

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我有另一个 Api 调用返回多个结果

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; }我需要一个 function 来查看所有多个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 Companies{ get; set; }在第二次 API 调用中,然后仅输出包含类似于public string Company { get; set; } public string Company { get; set; } public string Company { get; set; } in first API call.在第一个public string Company { get; set; }调用中。

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假设您将第一个 Api 调用的结果存储在ProjectMinInfo projectMinInfo中,并将结果或第二个 Api 存储在List<ResourcesAvailable> resourcesAvailableList中,现在您需要获取 ZA62666ZF63131ADCB499

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.好吧,这就是我所理解的:您有一个拥有公司的ProjectMinInfo实例。 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 .此外,您有一个ResourcesAvailable实例的集合,其中每个实例都有一些公司,用逗号和空格“,”分隔,例如Company1, Company2, Company3

You want to extract that instance of ResourcesAvailable that contains the company specified by the ProjectMinInfo instance, right?您想提取包含ProjectMinInfo实例指定的公司的ResourcesAvailable实例,对吗?

If so, this is the LINQ Query: Say project is an instance of type ProjectMinInfo , and resources - a list of ResourcesAvailable .如果是这样,这是 LINQ 查询:说projectProjectMinInfo类型的实例,资源 - 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哦,我看到这个问题在一月份被问到了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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