Separate a comma string and compare each value in the list

I have a csv file in the following format:

enter image description here

This rule sheet is loaded into an object List<DoctypeRule>, I use this list to retrieve DocType values ​​based on RuleCode and RuleValue. The DoctypeRule class is as follows:

public class DoctypeRule 
{
    public string doctype {get; set; }
    public string ruleCode {get; set; }
    public string ruleValue {get; set; }
}

Now, to get the rule, I use LINQ and pass the parameter.

DoctypeRule rule = new DoctypeRule();
rule = lsdoctypeRules.Find(r => r.docType == myparameter);

I also want to get a doctype with similar rules and save it in a list. Since some of the RuleValue will have comma-separated values, I cannot get doctrines with similar rules Example:

string ruleCode = rule.ruleCode;;
string ruleValue = rule.ruleValue;
List<string> lsruleValues = ruleValue.Split(',').ToList();

Now, to compile doctypes with a similar rule, I used

var siblingDoctypes = lsdoctypeRules
                    .Where(r => r.ruleValue == ruleValue && r.ruleCode == ruleCode)
                    .Select(x => x.docType);

This is useful for getting doctypes when RuleValue has only one value. But when the values ​​separated by commas are, I tried something like this

var siblingDoctypes = lsdoctypeRules                    
                    .Where(r => r.ruleValue.Split(',').ToList().Any(lsruleValues.Any().ToString()) && r.ruleCode == ruleCode)
                    .Select(x => x.docType);

, lsruleValues 4

A, B- I, - II,

Doctypes

, , B- I, - II.

+4
1

, , , . , , , lsruleValues . , :

var siblingDoctypes = 
    lsdoctypeRules                    
    .Where(r => r.ruleCode == ruleCode &&
        r.ruleValue
        .Split(new char[] { ',' })
        .Join(
            lsruleValues,
            x => x,
            y => y,
            (x, y) => x)
        .Any())
    .Select(x => x.docType);

, .

+3

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


All Articles