Avatar of Craig Lambie
Craig Lambie
Flag for Australia

asked on 

Use LINQ or Similar to Compare Array with Long String

Hi Experts,

Your thoughts and opinions please.
I am looking for a fast way to do something, and I am sure LINQ will have a great quick function I can't find to do it.

I have a List/ array [kitchen,breakfast,turkey sandwich]
And a several long strings [I went to the kitchen and had breakfast],[I had a turkey sandwhich for breakfast]

I would like to loop through the long strings and get the intersection of each from the array, I was thinking using LINQ would be easy.  

I did try this (where array is ListTags and long string is PageContent)
PageContent = PageContent + Environment.NewLine + dp.vchPageContent.ToString();
                    
                    // intCompanyID = t.intCompanyID != null ? (int)t.intCompanyID : 0 };


                    string[] parts = PageContent.ToString().Split(' ');

                    List<string> ListPageStrings = new List<string>(parts);

                    List<string> ListTags = (from dtg in db.DT_tblTags
                                             join drt in db.DT_tblRelDocuments_Tags on dtg.intTagID equals drt.intTagID
                                             select dtg.vchTag).ToList();

                    IEnumerable<string> LIntersection = ListTags.Intersect(ListPageStrings);

                    TagOperations to = new TagOperations();
                    TagElements te;

                    foreach (string tag in LIntersection)
                    {
                        //Add the tag to the Document Record
                        te = new TagElements();
                        te.intCompanyID = doc.intCompanyID;
                        te.intDocumentID = doc.intDocumentID;
                        te.intUserID = doc.intUserID;
                        te.vchTag = tag.ToString();

                        to.AddTagToDoc(te);
                    }

Open in new window


but I was using "split" and space to get two lists, which did't work when I had array members with more than one word.

I also thought about looping through for each member of the array and testing if it was in the long string using .contains, but that I feel will take a long time - there are 1050 array members in my Db)

So I am looking for a solution for the quickest way to find the intersection of the Long string and the List/ array?

Thoughts? Ideas?
.NET ProgrammingC#Microsoft SQL Server

Avatar of undefined
Last Comment
Craig Lambie

8/22/2022 - Mon