我正在尝试创建一个简单的代码来检索当前URL的字符串,如下所示:
string currentURL = HttpContext.Current.Request.Url.ToString();
Run Code Online (Sandbox Code Playgroud)
但是,我在运行代码时遇到错误: Object reference not set to an instance of an object.
我假设我必须创建一个HttpContext的实例.HttpContext的参数是HttpContext(HttpRequest request, HttpResponse response)或者HttpContext(HttpWorkerRequest wr).
是否有文档详细说明如何使用这些参数?我对C#很新,所以我不完全确定如何正确地实例化这个对象,并且没有找到任何有用的资源(包括MS库).
我有一个 XDocument 对象,我试图根据子元素的值获取直接父元素。
获取子元素的值一直没有问题,但我正在努力寻找仅获取父元素的正确方法。由于没有太多使用 XML,我怀疑解决方案很简单,而且我想得太多了。
本质上,基于下面的 XML,如果<Active>true</Active>我想要直接父元素(即<AlertNotification>)而不需要其他元素。
先感谢您。
XML 的示例
<?xml version="1.0" encoding="utf-16"?>
<Policies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLschema">
<PolicyID>1</PolicyID>
<EmailNotification>
<Active>false</Active>
</EmailNotification>
<AlertNotification>
<Active>true</Active>
</AlertNotification>
<AlarmEnabled>
<Active>false</Active>
</AlarmEnabled>
</Policies>
Run Code Online (Sandbox Code Playgroud) 为了比较两个List<String>并提取他们的差异,我使用Linq的Except.
即:
假设我想使用Linq比较以下两个相等的列表:
List1 = "0,1,2,2,3"
List2 = "0,1,2,3"
List<string> differences1 = List1.Except(List2).ToList();
List<string> differences2 = List2.Except(List1).ToList();
Run Code Online (Sandbox Code Playgroud)
differences1并且两个列表中differences2都没有项目2,但两个列表都不相等.我希望能够提取列表之间的所有差异,包括另一个没有的重复信息.
提取两个List<string>对象之间所有差异的最佳方法是什么?
说我有以下列表:
List 1:
{{"John", "Doe", "Tall", "Old"},
{"John", "Doe", "Short", "Old"},
{"Jane", "Doe", "Tall", "Young"},
{"Jane", "Doe", "Short", "Old"}}
Run Code Online (Sandbox Code Playgroud)
我想在列表中搜索{"John","Doe","Short","Old"}.
搜索此嵌套列表条目的最佳方法是什么,并确保我不会获得{"John","Doe","Tall","Old"}?
如果嵌套列表只包含一个string项而不是四个,我将使用LINQ展平列表并搜索结果List<string>.ie:
List<string> newList = oldList.SelectMany(x => x).Distinct().ToList();
newList.Contains("string");
Run Code Online (Sandbox Code Playgroud)
我可以为每个嵌套列表包含多个字符串项的列表做些类似的事情吗?