【发布时间】: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