Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please find below for more details.

I have a text with me ""1200005 # 28102016_0612 Inv.ACHExtractDetail POC.DiscountDetail Scanner Test1" & also a list of keyword

C#
keywords.Add(new KeywordCTIBO() { Keyword = "Scanner" });
          keywords.Add(new KeywordCTIBO() { Keyword = "Inv.ACHExtractDetail" });
          keywords.Add(new KeywordCTIBO() { Keyword = "POC.DiscountDetail" });


What i require is i need to extract "Inv.ACHExtractDetail" from text since it is present in the list. What i mean is the first occurrence of keyword from text if and only if it exist in the list.

Below code works fine but i am looking much better if possible without using loops.

C#
List<KeywordCTIBO> keywords = new List<KeywordCTIBO>();
           KeywordCTIBO matchedKeyword = null;
           keywords.Add(new KeywordCTIBO() { Keyword = "Scanner" });
           keywords.Add(new KeywordCTIBO() { Keyword = "Inv.ACHExtractDetail" });
           keywords.Add(new KeywordCTIBO() { Keyword = "POC.DiscountDetail" });

           string text = "1200005 # 28102016_0612 Inv.ACHExtractDetail POC.DiscountDetail Scanner Test1";
           //var matchedKeyWords = keywords.Where(f => text.ToLower().Split(',', ' ', ';').Contains(f.Keyword.ToLower())).Select(p => new { KeyWordBO = p, Index = text.ToLower().IndexOf(p.Keyword.ToLower()) });
           //string[] arr = text.ToLower().Split(',', ' ', ';');
           string [] arr = text.ToLower().Split(',', ' ', ';');
           string s = string.Empty;
           foreach(var r in arr){
               var matchedKeyWords = keywords.Where(f => r.Contains(f.Keyword.ToLower())).Select(p => new { KeyWordBO = p, Index = text.ToLower().IndexOf(p.Keyword.ToLower()) });
               foreach (var f in matchedKeyWords)
               {
                   s = f.KeyWordBO.Keyword;
               }
           }
       }
       public class KeywordCTIBO
       {
           public string Keyword { get; set; }
       }


What I have tried:

C#
List<KeywordCTIBO> keywords = new List<KeywordCTIBO>();
            KeywordCTIBO matchedKeyword = null;
            keywords.Add(new KeywordCTIBO() { Keyword = "Scanner" });
            keywords.Add(new KeywordCTIBO() { Keyword = "Inv.ACHExtractDetail" });
            keywords.Add(new KeywordCTIBO() { Keyword = "POC.DiscountDetail" });
          
            string text = "1200005 # 28102016_0612 Inv.ACHExtractDetail POC.DiscountDetail Scanner Test1";
            //var matchedKeyWords = keywords.Where(f => text.ToLower().Split(',', ' ', ';').Contains(f.Keyword.ToLower())).Select(p => new { KeyWordBO = p, Index = text.ToLower().IndexOf(p.Keyword.ToLower()) });
            //string[] arr = text.ToLower().Split(',', ' ', ';');
            string [] arr = text.ToLower().Split(',', ' ', ';');
            string s = string.Empty;
            foreach(var r in arr){
                var matchedKeyWords = keywords.Where(f => r.Contains(f.Keyword.ToLower())).Select(p => new { KeyWordBO = p, Index = text.ToLower().IndexOf(p.Keyword.ToLower()) });
                foreach (var f in matchedKeyWords)
                {
                    s = f.KeyWordBO.Keyword;
                }
            }
        }
        public class KeywordCTIBO
        {
            public string Keyword { get; set; }
        }
        }
Posted
Updated 30-Oct-16 20:04pm

1 solution

Your concepts/methods are good. If you just want optimisation, I'd look at the following points:

  • r.Contains(.. in your Linq - is Contains necessary? Were you thinking arr.Contains? If so, may be worth while putting arr into a HashSet, and using a case-insensitive comparer rather than converting everything ToLower on each iteration.
  • Briefly mentioned above - you are converting everything ToLower() on each iteration. Either convert things to lower case before the Linq statement, or use case-insensitive comparers.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900