【问题标题】:Exluding Items that are in my Exceptions List LINQ to SQL排除我的例外列表 LINQ to SQL 中的项目
【发布时间】:2014-05-19 21:38:08
【问题描述】:

我有一个 linq 查询,它返回一个 URL 列表。我不包括以许多扩展名(例如(bat、cmd、...))结尾的任何 URL。我在查询本身中使用了许多 && 语句,但我更愿意使用定义的 myExceptions 列表来给我更多的灵活性,因为这个列表可能会改变并且可能会在我的项目的其他地方使用。

我想替换我的查询部分

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

使用例外列表以获得更大的灵活性,例如

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

阅读其他 stackoverflow 帖子,我认为我可以使用 except 扩展方法或任何带有 Contains 的方法,就像我在整个网站上看到的那样

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

但在这种情况下,我不知道如何修改我的查询来做到这一点。任何想法都会非常感激。

这是我现有的查询:

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

【问题讨论】:

    标签: linq linq-to-sql


    【解决方案1】:

    看看这是否有效:

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

    【讨论】:

    • 感谢您的建议,但不幸的是,这会导致错误消息“Local Sequence cannot be used in LINQ to SQL implementation of query operation except the contains operator”
    • 好的,它对我有用,因为我使用的是实体框架
    • 知道了!按照stackoverflow.com/questions/1712105/… 的建议,我使用 contains 方法而不是 Any 稍微修改了您建议的查询。
    • 我正要改成: //... !myExepections.Contains(x => myURL.myURL.Trim().Substring((myURL.myURL.length - 3), 3 )) && myURL.myURL.Length>0 //...
    • 我已经用适用于实体框架和 Linq 2 SQL 的解决方案更新了答案,我不确定性能,但它们可以工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-22
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多