Question

I have a linq query which is return a list of URLS. I'm an excluding any URLS that end with a number of extensions such as (bat, cmd, ...). I have this working with a number of && statements coded into the query itself but would rather use a defined myExceptions list to give me more flexibility as this list may change and may be used elsewhere in my project.

I want to replace the part of my query

  && (!myURL.myURL.Trim().EndsWith("exe")
                                           && !myURL.myURL.Trim().EndsWith("cmd")
                                           && !myURL.myURL.Trim().EndsWith("lnk")
                                           && !myURL.myURL.Trim().EndsWith("bat")                                              
                                           && myURL.myURL.Length > 0 
                                           ))

To use a list of exceptions for more flexibility like

 List<string> myExepections = new List<string> { "exe", "cmd", "lnk", "bat" };

Reading through other stackoverflow posts I presume I can use the except extension method or Any method with Contains as I've seen throughout the site like

Linq - How to select items from a list that contains only items of another list?

But I don't know how to modify my query to do that in this case. Any ideas would be greatly appreciative.

Here's my existing query:

var results = myDataClass.LinksTabke.OrderBy(myURL => myURL.url_Description)
                                   .Where(
                                       myURL =>
                                       myURL.PageID.Equals(QueryStringID)
                                       && (!myURL.myURL.Trim().EndsWith("exe")
                                           && !myURL.myURL.Trim().EndsWith("cmd")
                                           && !myURL.myURL.Trim().EndsWith("lnk")
                                           && !myURL.myURL.Trim().EndsWith("bat")                                              
                                           && myURL.myURL.Length > 0 
                                           ))
                                   .Select(
                                       myURL =>
                                       new results
                                           {
                                               mySectionID = myURL.myTable.mySectionID,                                                
                                               myDescText = myURL.myTable.Desc,   LinkUrl =  myURL.myURL
                                           }).Distinct();
        return results.ToList();
Était-ce utile?

La solution

See if this works:

var results = myDataClass.LinksTabke.OrderBy(myURL => myURL.url_Description)
                                   .Where(
                                       myURL => myURL.PageID.Equals(QueryStringID)

                                       &&
                                       //Works for entity framework
                                       !myExepections.Any(x => x == myURL.myURL.Trim().Substring((myURL.myURL.length - 3), 3)) && myURL.myURL.Length>0)

                                       //Works for Linq 2 SQL and EF                  
                                       !myExepections.Contains(x => myURL.myURL.Trim().Substring((myURL.myURL.length - 3), 3))
                                       && myURL.myURL.Length>0)

                                   .Select(
                                       myURL =>
                                       new results
                                       {
                                           mySectionID = myURL.myTable.mySectionID,
                                           myDescText = myURL.myTable.Desc,
                                           LinkUrl = myURL.myURL
                                       }).Distinct();
           return results.ToList();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top