小编Aag*_*nor的帖子

Dictionary.Cld的高运行时添加了大量的项目

我有一个C#-Application,它将来自TextFile的数据存储在Dictionary-Object中.要存储的数据量可能相当大,因此插入条目需要花费大量时间.由于内部数组的大小调整存储了Dictionary的数据,因此在Dictionary中有许多项目会变得更糟.因此,我使用将要添加的项目数量初始化词典,但这对速度没有影响.

这是我的功能:

private Dictionary<IdPair, Edge> AddEdgesToExistingNodes(HashSet<NodeConnection> connections)
{
  Dictionary<IdPair, Edge> resultSet = new Dictionary<IdPair, Edge>(connections.Count);

  foreach (NodeConnection con in connections)
  {
    ...
    resultSet.Add(nodeIdPair, newEdge);
  }

  return resultSet;
}
Run Code Online (Sandbox Code Playgroud)

在我的测试中,我插入~300k项目.我用ANTS Performance Profiler检查了运行时间,发现当我用所需的大小初始化Dictionary时,resultSet.Add(...)的平均时间不会改变.它与我用新的Dictionary()初始化Dictionary时相同; (每次添加平均约0.256毫秒).这肯定是由字典中的数据量引起的(尽管我用所需的大小初始化它).对于前20k项,每个项的Add的平均时间为0.03 ms.

任何想法,如何使添加操作更快?

先谢谢你,弗兰克

这是我的IdPair-Struct:

public struct IdPair
{
  public int id1;
  public int id2;

  public IdPair(int oneId, int anotherId)
  {
    if (oneId > anotherId)
    {
      id1 = anotherId;
      id2 = oneId;
    }
    else if (anotherId > oneId)
    {
      id1 = oneId;
      id2 = anotherId;
    }
    else
      throw new …
Run Code Online (Sandbox Code Playgroud)

c# dictionary

5
推荐指数
2
解决办法
1495
查看次数

Silverlight 4:当项目缩小时,列表框不会缩小

从这个问题中,我将问题深入到列表框中,当Listbox-Items缩小时,该列表框不会调整大小.当项目的大小增加时,它会相应地调整大小,但是当项目的大小减小时,它不会缩小.

项目可以增长/缩小,因为包含文本框的项目会随输入调整大小.

Jeremiah建议用更多代码开始一个新问题,所以我们走了:

我们的邪恶列表框是UserControl的一部分,它包含一个带有Label(Horizo​​ntalAlignment = Center)的StackPanel,列表框(HA = Left)和一个Button(HA = Right).listbox-items被数据链接到ObservableCollection

您将识别ListBox和ListBoxItems上的漂亮BackgroundColors.我用它们来告诉项目或列表框本身不会缩小.我发现,项目缩小,但列表框没有.

好的,这是我的UserControl的代码:

<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left">
  <StackPanel.Background>
    <SolidColorBrush Color="{StaticResource ColorBasicDark}"/>
  </StackPanel.Background>

  <sdk:Label x:Name="LabelServiceName" FontSize="{StaticResource FontSizeMedium}" Margin="2" HorizontalAlignment="Center" Content="LabelServiceName">
    <sdk:Label.Foreground>
      <SolidColorBrush Color="{StaticResource ColorBasicLight}"/>
    </sdk:Label.Foreground>
  </sdk:Label>

  <ListBox x:Name="ListBoxCharacteristics" BorderBrush="{x:Null}" Margin="0" HorizontalContentAlignment="Left" FontSize="9.333" HorizontalAlignment="Left">
    <ListBox.Foreground>
      <SolidColorBrush Color="{StaticResource ColorBasicLight}"/>
    </ListBox.Foreground>

    <!-- DataTemplate to display the content -->
    <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel x:Name="StackPanelBorder" Orientation="Horizontal" HorizontalAlignment="Left">
          <TextBox x:Name="TextBoxCharacteristicName" Style="{StaticResource InputTextBox}" Text="{Binding Name}" />
          <TextBox x:Name="TextBoxSep" Style="{StaticResource ReadOnlyTextBox}" Text="=" />
          <TextBox x:Name="TextBoxFuncOrValue" Style="{StaticResource InputTextBox}" Text="{Binding Value.Text}" /> …
Run Code Online (Sandbox Code Playgroud)

silverlight listbox

5
推荐指数
1
解决办法
1975
查看次数

在ObservableCollection上使用Lambda表达式

在我的Silverlight 4应用程序中,我有一个ObservableCollection,它由一个类的对象组成,并由一个接口定义:

interface myInterface()
{
  string Name { get; set; }
  string Value { get; set; }
} 

class myClass() : myInterface
{
  ...
}

ObservableCollection<myInterface> _collection;
Run Code Online (Sandbox Code Playgroud)

在向集合添加新元素之前,我想确保Name-Property在当前集合元素中不存在.由于我无法使用contains,我目前遍历所有元素并手动检查每个元素.

private bool CollectionContainsElement(string name2CheckAgainst)
{
  foreach (myInterface item in _collection)
    if (item.Name.Equals(name2CheckAgainst))
      return true;

  return false;
}
Run Code Online (Sandbox Code Playgroud)

我已经读过这也可以通过Lambda Expression实现,所以我写了以下内容:

if (!_collection.Contains(p => p.Name == name2CheckAgainst))
{
  ...
Run Code Online (Sandbox Code Playgroud)

但现在我收到一个错误,说"lambda表达式无法转换为Type"myInterface",因为它不是委托类型".(措辞可能有所不同,因为我从德文版翻译了它)

我不确定我需要改变什么才能让它发挥作用.using System.Linq;已经包括了.第二个问题(或者可能是主要问题):我已经读过,运行时从O(1)变为Contains() - 方法变为O(n) - 这不比我当前的检查快.那么将它改为使用lambda甚至是否有意义?最后,在我的班级中检查现有的Name-Property可能还有另一种方法吗?

先谢谢你,
弗兰克

c# collections lambda contains

5
推荐指数
1
解决办法
1万
查看次数

处理 SPARQL-Filter 表达式中的特殊字符

我访问 dbpedia[1] 的 SPARQL-Endpoint 以获取给定城市的 URI。我使用以下查询来实现此目的:

\n\n
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\nPREFIX dbpedia: <http://dbpedia.org/ontology/> \nselect distinct * where \n  {?uri rdfs:label ?label. \n  FILTER (REGEX(STR(?label), "^K\xc3\xb6ln$", "i")). \n  ?uri a dbpedia:PopulatedPlace.\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

如果我查询一个没有德语变音符号的城市,一切正常,但如果有变音符号,我什么也得不到。当通过代码执行此查询时,我什至收到 406 错误(不可接受)

\n\n

知道如何处理针对 dbpedia 的 SPARQL 查询中的元音变音吗?

\n\n

预先感谢,
\n弗兰克

\n\n

[1] http://dbpedia.org/sparql

\n

regex sparql dbpedia

5
推荐指数
1
解决办法
2152
查看次数

SPARQL-Queries中OPTIONAL关键字的替代方案?

我有一个sparql-Query,它询问给定类型的URI的某些属性.由于我不确定,这些属性是否存在,我使用OPTIONAL Keyword:

PREFIX mbo: <http://creativeartefact.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
SELECT * WHERE {
  ?uri a mbo:LiveMusicEvent. 
    OPTIONAL {?uri rdfs:label ?label}. 
    OPTIONAL {?uri mbo:organisedBy ?organiser}. 
    OPTIONAL {?uri mbo:takesPlaceAt ?venue}. 
    OPTIONAL {?uri mbo:begin ?begin}. 
    OPTIONAL {?uri mbo:end ?end}. 
}
Run Code Online (Sandbox Code Playgroud)

当我对我的SPARQL-Endpoint(Virtuoso Server)运行此查询时,我收到错误:

Virtuoso 42000错误估计执行时间-721420288(秒)超过400(秒)的限制.

当我减少OPTIONAL子句时,在第一个删除子句之后,估计的执行时间是4106秒,当我删除两个子句时,执行查询(并立即返回值).

我看不出,为什么估计的执行时间与其他可选条款一样暴涨,但也许我只是使用错误的构造查询?

rdf sparql virtuoso

5
推荐指数
1
解决办法
1047
查看次数

使用 gnuplot 根据列值绘制多个图表

我有以下数据,但我不想使用 GNUPLOT 绘制这些数据:

#TIME #VALUE #SOURCE
1 100 A
1 88 B
2 115 A
2 100 B
3 130 A
3 210 B
Run Code Online (Sandbox Code Playgroud)

我想根据 #SOURCE 列的值绘制两条线。A一行, B一行。GNUPLOT 是否可以实现这一点?如果可以,如何实现?

是否还可以根据列 #TIME 绘制列 #VALUE 的总和?意味着,对于 #TIME 中的所有相等条目,#VALUE 中的值将被求和。

预先感谢,弗兰克

gnuplot

5
推荐指数
1
解决办法
2096
查看次数

RestSharp 并忽略 SSL 证书中的错误

我尝试在显然没有有效 ssl 证书的服务器上使用 RestSharp 启动 REST 请求,因为我收到错误

底层连接已关闭:无法建立 SSL/TLS 安全通道的信任关系。

我发现了这个问题,但提供的解决方案,虽然看起来很简单,但不起作用,我仍然收到错误。这是我的代码,有什么想法可能有问题吗?

private void Test_REST()
{
  var client = new RestClient("https://online.gema.de");

  // both versions not working
  ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
  client.RemoteCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;

  // execute the request
  IRestResponse response = client.Execute(GEMA_Authorize());
  var content = response.Content; // raw content as string
}

private RestRequest GEMA_Authorize()
{
  RestRequest request = new RestRequest("api/v1/authorize", Method.GET);
  request.AddParameter("response_type", "token"); 
  request.AddParameter("client_id", "test_client");
  request.AddHeader("Accept", "application/json");
  request.AddHeader("Content-Type", "application/x-www-form-urlencoded");

  return …
Run Code Online (Sandbox Code Playgroud)

rest ssl restsharp

5
推荐指数
1
解决办法
1万
查看次数

C#和XPath - 如何查询

我有一个XML文件,它具有以下结构:

<mediawiki ...>
  <siteinfo>...</siteinfo>
  <page>
    <title>The Title</title>
    <id>42</id>
    ...
  </page>
  ... more page items ...
</mediawiki>
Run Code Online (Sandbox Code Playgroud)

我稍微了解一下XPath-Query并编写了以下代码:

_xmlArticlesDocument = new XmlDocument();
_xmlArticlesDocument.Load(xmlArticlesFileName);
Run Code Online (Sandbox Code Playgroud)

现在我想抓住一个具有给定id-Subelement的页面元素.所以我写道:

XmlNode node = _xmlArticlesDocument.DocumentElement.SelectSingleNode
               ("//mediawiki/page[id=" + id.ToString() + "]");
Run Code Online (Sandbox Code Playgroud)

但节点始终为空.我已经尝试了几个查询,包括"page/id","page","/ page","// page"来获取任何内容,但节点始终为null.我通过快速检查检查了_xmlArticlesDocument变量,它包含具有预期结构的正确XML文件.

在我看来,我错过了一些非常基本的东西,但不知道是什么.也许这里有人有想法?

先谢谢你,弗兰克

c# xpath

3
推荐指数
1
解决办法
8199
查看次数

为foreach派生的类启用foreach

我有一个派生自Dictionary的类.我需要这个类来模拟HashSet,因为Silverlight不知道HashSets并且我的类大量使用HashSet.所以我决定用字典交换HashSet.为了进一步将我的类与所有HashSet-Objects一起使用,我尝试创建一个自定义的HashSet类,它派生自Dictionary并覆盖所有相关方法,如Add-method:

class HashSet<T> : Dictionary<T, object>
{

    public override void Add(T element)
    {
        base.Add(element, null);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我需要为我的新HashSet类启用foreach-loop.显然,我的类在foreach循环中返回一个KeyValuePair,但我需要T作为返回类型.谁能告诉我,我需要覆盖Dictionary基类的内容和方式?

先谢谢你,弗兰克

c# inheritance dictionary

3
推荐指数
1
解决办法
2546
查看次数

删除WCF服务答案的大小限制

我创建了一个WCF服务,它在W2008-Server上运行一个操作并返回一个数据结构.问题是,这个结果可能比标准服务配置似乎更接受.所以我试图增加(或删除)这个最大尺寸,但似乎,我没有找到正确的属性.

在WCF的App.config中,我更改了basicHttpBinding的以下值:

  • MaxBufferPoolSize - > 6553600
  • MaxBufferSize - > 6553600
  • MaxReceiveMessageSize - > 6553600

ReaderQuotas:

  • MaxArrayLenght - > 0
  • MaxBytesPerRead - > 0
  • MaxDepth - > 0
  • MaxNameTableCharCount - > 0
  • MaxStringContentLength - > 0

然后我启动WCF-Testclient来调用该服务.我确保basicHttpBinding的属性值等于配置中的值.当我以某种方式调用服务时,结果集相当小,一切正常.但是当这个尺寸增加时,我最终会得到错误(翻译自德语):

接收http :// localhost:8731/Design_Time_Addresses/DiscoDataSource/Service1 /的 http应答时出错.可能的原因:Endpointbinding不使用HTTP协议,或者服务器取消了HTTP-requestcontext.

服务器堆栈跟踪:

在System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException,HttpWebRequest request,HttpAbortReason abortReason)

at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

在System.ServiceModel.Channels.RequestChannel.Request(消息消息,TimeSpan超时)

在System.ServiceModel.Dispatcher.RequestChannelBinder.Request(消息消息,TimeSpan超时)

在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs,TimeSpan timeout)

在System.ServiceModel.Channels.ServiceChannel.Call(String action,Boolean oneway,ProxyOperationRuntime operation,Object [] ins,Object [] outs)

在System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall,ProxyOperationRuntime操作)

在System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage消息)

我想配置服务以返回结果,无论大小.谁知道,我需要更改配置?

先谢谢你,弗兰克

c# wcf

3
推荐指数
1
解决办法
4693
查看次数