Link to home
Create AccountLog in
Avatar of lulu50
lulu50Flag for United States of America

asked on

Search for a range

I need your help please.  


My search "Not Range" is not pulling my data.
and if I enter in the textbox G2002 or G2001-G2004
Record #1 will not come up.
the search will only get the first range but not the second one.


if the user enter in the GroupTxt : G2001  
output record:
RuleDetailID                       ConditionAttributesAndValues

1                                           <br> IF Px Between Range (H0000 - H9999)&nbsp;<br> AND Px Between Range (G2000 - G5000)

4                                           if Member is ( URMBT ) and<br>Place of Service is ( Inpatient Hospital&nbsp;&nbsp; ) and <br>Case Type is ( Medical or Surgical or BH )and<br>AND Dx Between  
                                             Range (F01-F99) <br> AND Px Between Range (G2000 - G5000)


if the user enter in the GroupTxt : Not G2001  
output record:
RuleDetailID                       ConditionAttributesAndValues                      

5                                           IF Member Source Type Concept = 2&nbsp; (BCN)<br> AND Px Between Range (H0000 - H9999)&nbsp;<br> AND Px Not Between Range (G2000 - G5000)
                                             &nbsp;&nbsp;<br> AND Dx Not Between Range (D20 - D60)

or if the user enter in the GroupTxt : Not (G2001-G2020)  
output record:
RuleDetailID                       ConditionAttributesAndValues                      

5                                           IF Member Source Type Concept = 2&nbsp; (BCN)<br> AND Px Between Range (H0000 - H9999)&nbsp;<br> AND Px Not Between Range (G2000 - G5000)
                                             &nbsp;&nbsp;<br> AND Dx Not Between Range (D20 - D60)


or if the user enter in the GroupTxt : Not D20  
output record:
RuleDetailID                       ConditionAttributesAndValues                      

5                                           IF Member Source Type Concept = 2&nbsp; (BCN)<br> AND Px Between Range (H0000 - H9999)&nbsp;<br> AND Px Not Between Range (G2000 - G5000)
                                             &nbsp;&nbsp;<br> AND Dx Not Between Range (D20 - D60)

3                                           if Members Group ID is in the list ( 71796)&nbsp; <br><br>  AND Dx Between Range (C00- C90) AND Dx Not Between Range (D19 - D90)


or if the user enter in the GroupTxt : G30  
output record:
RuleDetailID                       ConditionAttributesAndValues        
             
2                                           <br> IF Dx Between Range (G23- G40)&nbsp;<br> AND Dx Between Range (M23- M50)



or if the user enter in the GroupTxt : M25-M30  
output record:
RuleDetailID                       ConditionAttributesAndValues        
             
2                                           <br> IF Dx Between Range (G23- G40)&nbsp;<br> AND Dx Between Range (M23- M50)



Table : RuleDetailRepo
Fields: ConditionAttributesAndValues

RuleDetailId                        ConditionAttributesAndValues

1                                           <br> IF Px Between Range (H0000 - H9999)&nbsp;<br> AND Px Between Range (G2000 - G5000) 

2                                           <br> IF Dx Between Range (G23- G40)&nbsp;<br> AND Dx Between Range (M23- M50) 

3                                           if Members Group ID is in the list ( 71796)&nbsp; <br><br>  AND Dx Between Range (C00- C90) AND Dx Not Between Range (D19 - D90) 

4                                           if Member is ( URMBT ) and<br>Place of Service is ( Inpatient Hospital&nbsp;&nbsp; ) and <br>Case Type is ( Medical or Surgical or BH )and<br>AND Dx Between   
                                             Range (F01-F99) <br> AND Px Between Range (G2000 - G5000) 

5                                           IF Member Source Type Concept = 2&nbsp; (BCN)<br> AND Px Between Range (H0000 - H9999)&nbsp;<br> AND Px Not Between Range (G2000 - G5000)
                                             &nbsp;&nbsp;<br> AND Dx Not Between Range (D20 - D60) 



                     list = _unitOfWorkCABusinessRules.RuleDetailRepo.GetAll().ToList();

                    //Filter Group Txt   
                    if (model.GroupTxt != null)
                        list = list.Where(x => InListOrRange(model.GroupTxt, x.ConditionAttributesAndValues));



    private bool InListOrRange(string search, string source)
        {
            var result = default(bool);
            var start = default(int);
            var end = default(int);
            var searchRange = default(Dictionary<string, IEnumerable<int>>);
            var sourceRange = default(Dictionary<string, IEnumerable<int>>);
            var list = default(IEnumerable<string>);
            try
            {
                // Replace the whitespace for easier parsing
                search = search.Replace(" ", "");
                source = source.Replace(" ", "");

                // Get our ranges
                start = source.IndexOf("BetweenRange(", StringComparison.OrdinalIgnoreCase) + "BetweenRange(".Length;
                end = source.IndexOf(")", start);
                searchRange = GetRangeDictionary(search);
                sourceRange = GetRangeDictionary(source.Substring(start, end - start));

                // Get our list
                start = source.IndexOf("(") != start ? source.IndexOf('(') + 1 : -1;
                if (start != -1)
                {
                    end = source.IndexOf(")", start);
                }
                list = start != -1 ? source.Substring(start, end - start).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Replace("<br>", "")) : new string[] { };

                result = (from lhs in searchRange
                          from rhs in sourceRange
                          where Equals(lhs.Key, rhs.Key)
                          select lhs.Value.Intersect(rhs.Value).Any()).Any(x => x) || list.Any(x => string.Equals(search, x, StringComparison.OrdinalIgnoreCase));
            }
            catch (Exception e)
            {
                //Console.WriteLine($"Message: {ex.Message}{Environment.NewLine}{ex.StackTrace}");
                result = false;
            }

            return result;
        }





        private Dictionary<string, IEnumerable<int>> GetRangeDictionary(string source)
        {
            var result = default(Dictionary<string, IEnumerable<int>>);
            try
            {
                result = source.Split('-')
                    .Select(x => new KeyValuePair<string, int>(Regex.Match(x, @"\D+")?.Value ?? string.Empty, Convert.ToInt32(Regex.Match(x, @"\d+")?.Value ?? "0")))
                    .OrderBy(x => x.Value)
                    .GroupBy(x => x.Key)
                    .Select(x => new KeyValuePair<string, IEnumerable<int>>(x.Key, Enumerable.Range(x.First().Value, x.LastOrDefault().Value == 0 ? 1 : x.Last().Value - x.First().Value + 1)))
                    .ToDictionary(x => x.Key, x => x.Value);
            }
            catch (Exception e)
            {
                //Console.WriteLine($"Message: {ex.Message}{Environment.NewLine}{ex.StackTrace}");
                result = new Dictionary<string, IEnumerable<int>>();
            }

            return result;
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer