4

I have a csv file in the below format :

enter image description here

This Rule sheet is loaded in a List<DoctypeRule> object, I am using this list to fetch the DocType values based on the RuleCode and RuleValue. The DoctypeRule class looks like this

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

Now, to fetch a rule i use a LINQ and pass a parameter.

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

Also i want to fetch the doctype with similar rules and store in a list. Since some of the RuleValue will have comma separated values, i am not able to fetch the doctypes with similar rules Example :

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

Now to collect doctypes with similar rule i used

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

This is good to fetch doctypes when RuleValue is having only one value. But when comma separated values are there 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);

What is expected is if lsruleValues has 4 items

Title Policy Schedule A,Title Policy Schedule B-Part I,Title Policy Schedule B-Part II,Title Policy

Then sibling Doctypes should be a list of

Title Endorsement, Title Policy Schedule A,Title Policy Schedule B-Part I,Title Policy Schedule B-Part II.

Maverick
  • 1,396
  • 5
  • 22
  • 42
  • Are you aware that your scheme vialotes database normalization? Please read: http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad –  Oct 16 '15 at 23:16

1 Answers1

3

Okay, I think I have wrapped my head around what you are trying to do. What you really want to do is check if lsruleValues contains any of the rule values. I believe this will do the trick:

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);

You may want to consider using a case-insensitive string comparison too.

Jason Boyd
  • 6,839
  • 4
  • 29
  • 47
  • One word of caution though: The performance is going to be 0(n) - depending on the size on position of occurence you should rather look for a splitting and joining or using a `HashSet` (whose performance is better, but built-up time might be worse - you'll have to find the sweet spot). –  Oct 16 '15 at 14:23
  • 1
    I'm not aware of a `Join` overload that doesn't require the key selectors and the result selector. – juharr Oct 16 '15 at 14:34
  • @juharr - You are right. I don't know what I was thinking. – Jason Boyd Oct 16 '15 at 14:42
  • @JasonBoyd thanks for the response but this join doesn't have a keyselector and resultselector, may be thats the reason it's giving "No overload of join takes 1 argument" issue. – Maverick Oct 16 '15 at 14:43
  • @JasonBoyd your edit on join() parameters now works. Thanks – Maverick Oct 16 '15 at 18:25