我正在尝试从字典中构建饼图.在我显示饼图之前,我想整理数据.我正在删除任何小于饼的5%的饼图,并将它们放入"其他"饼图中.但是我Collection was modified; enumeration operation may not execute在运行时遇到异常.
我理解为什么你不能在迭代它们时添加或删除字典中的项目.但是我不明白为什么你不能简单地改变foreach循环中现有键的值.
任何建议:修复我的代码,将不胜感激.
Dictionary<string, int> colStates = new Dictionary<string,int>();
// ...
// Some code to populate colStates dictionary
// ...
int OtherCount = 0;
foreach(string key in colStates.Keys)
{
double Percent = colStates[key] / TotalCount;
if (Percent < 0.05)
{
OtherCount += colStates[key];
colStates[key] = 0;
}
}
colStates.Add("Other", OtherCount);
Run Code Online (Sandbox Code Playgroud) string str1 = "12345ABC...\\...ABC100000";
// Hypothetically huge string of 100000 + Unicode Chars
str1 = str1.Replace("1", string.Empty);
str1 = str1.Replace("22", string.Empty);
str1 = str1.Replace("656", string.Empty);
str1 = str1.Replace("77ABC", string.Empty);
// ... this replace anti-pattern might happen with upto 50 consecutive lines of code.
str1 = str1.Replace("ABCDEFGHIJD", string.Empty);
Run Code Online (Sandbox Code Playgroud)
我继承了一些与上面的代码片段相同的代码.它需要一个巨大的字符串,并从大字符串中替换(删除)常量较小的字符串.
我相信这是一个非常耗费内存的过程,因为每个替换都会在内存中分配新的大型不可变字符串,等待通过GC死亡.
1.更换这些值的最快方法是什么,忽略内存问题?
2.实现相同结果的最有效的内存方式是什么?
我希望这些是相同的答案!
在这些目标之间适合某些地方的实用解决方案也值得赞赏.
假设:
我正在为我的ASP.NET Web API帮助页面使用XML文档,如下所示.我想知道是否有办法在评论中包含html,使其在网页上呈现,而不是被删除/忽略/转义.
具体来说,我正在寻找一种创建换行符的方法,但是能够创建项目符号列表等等会很棒!
Ex /我希望能够做到这样的事情:
/// <summary>
/// CRUD operations for SalesDocument<br/>
/// This is a new line
/// </summary>
[RoutePrefix("api/SalesDocument")]
public partial class SalesDocumentController : ApiController
Run Code Online (Sandbox Code Playgroud)
并将它显示在帮助页面上,如下所示:
CRUD operations for SalesDocument
This is a new line.
Run Code Online (Sandbox Code Playgroud)
而不是这样:(在这种情况下,<br/>以某种方式被删除 - 如果我尝试使用<p>标签,它们只是被转义)
CRUD operations for SalesDocument This is a new line.
Run Code Online (Sandbox Code Playgroud)
*我已经尝试过<para>工具提示的多个帖子所建议的标记,但这不适用于我的帮助页面.
任何建议都非常感谢!
pika教程中的所有示例都以客户端调用结束start_consuming(),这会启动无限循环.这些例子对我有用.
但是,我不希望我的客户端永远运行.相反,我需要我的客户端消耗消息一段时间,例如15分钟,然后停止.
我该如何做到这一点?
没有解释整个上下文,我的问题基本上是这样的:
我在Windows窗体上有一个datagridview,它绑定到Entity Framework DbSet : dbSet<TEntity>.Local.ToBindingList().
如果我将datagridview的ReadOnly属性设置为true(在设计视图中),然后在我的代码中包含此语句:
myDataGridView.Rows[rowIndex].ReadOnly = false;
Run Code Online (Sandbox Code Playgroud)
它直接通过而不改变价值!(不,我的数据源不是只读的.)
循环遍历行中的单元格并单独设置每个单元格的ReadOnly属性也不起作用:
foreach (DataGridViewCell cell in myDataGridView.Rows[rowIndex].Cells)
{
cell.ReadOnly = false;
}
Run Code Online (Sandbox Code Playgroud)
.ReadOnly - Gets or sets a value indicating whether the user can edit the cells of the DataGridView control. 但它表现得像是因为某种原因无法设定.
这有可能发生吗?我在想,也许datagridview的ReadOnly属性会覆盖对特定行/单元格所做的任何更改(即整个表单必须是readonly或不是),但显然这适用于某人......
还有其他方法可以实现我的最终目标,但我想知道为什么我不能改变这个属性,因为这对我的情况来说是最简单的解决方案.
让我试着澄清一下我的问题:
同样,我知道还有其他方法可以实现我的最终目标,但想要回答这个问题:
抱歉原始示例,但要轻松复制问题,创建一个WinForm应用程序,在其上抛出一个datagridview,并将此代码传递到您的项目中:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{ …Run Code Online (Sandbox Code Playgroud) 我们正在使用实体框架代码优先,我遇到了在我们不希望的情况下尝试回滚插入,更新和删除实体更改的问题SaveChanges().
具体来说,我有一个datagridview,我用它作为TableEditor,来更改一些辅助表.datagridview绑定到DbSet<TEntity>.
我的回滚更新似乎工作正常,我只是将currentValues设置回其OriginalValues,并将状态更改为unchanged.
当一个记录插入到gridview中(但没有保存更改)时,它永远不会出现在实体类中,我再也看不到了......所以我猜它不会进入dbSet,也没有回滚是需要这个吗?
但我的主要问题在于删除:
根据我的理解,当记录被"删除"(例如tableData.Remove(currentItem);)时,它被简单标记为删除,直到调用SaveChanges.因此,如果我将状态从deleted返回更改为unchanged,那应该处理回滚,对吧?
那么,记录确实再次显示,但记录的导航属性已经消失!(即包含外键的列和与其他实体的必需关系).为什么是这样??!
这是我到目前为止:
public void RollbackChanges(DbEntityEntry entry)
{
if (entry.State == EntityState.Modified)
{
foreach (var propertyName in entry.OriginalValues.PropertyNames)
{
entry.CurrentValues[propertyName] = entry.OriginalValues[propertyName];
}
entry.State = EntityState.Unchanged;
}
else if (entry.State == EntityState.Deleted)
{
entry.State = EntityState.Unchanged;
}
else if ((entry.State == EntityState.Added) || (entry.State == EntityState.Detached))
{
MessageBox.Show("I don't think this ever happens?");
}
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
foreach (var entity …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 PDFSharp 和此代码(我在此处找到)将两个创建的 PDF 文件连接到一个新的 PDF :
// Open the output document
PdfDocument outputDocument = new PdfDocument();
// Iterate files
foreach (string file in files)
{
// Open the document to import pages from it.
PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import);
// Iterate pages
int count = inputDocument.PageCount;
for (int idx = 0; idx < count; idx++)
{
// Get the page from the external document...
PdfPage page = inputDocument.Pages[idx];
// ...and add it to the output document.
outputDocument.AddPage(page); …Run Code Online (Sandbox Code Playgroud) 谁能告诉我这里发生了什么?
byte[] stamp = new byte[]{0,0,0,0,0,1,177,115};
string serialize = System.Text.Encoding.UTF8.GetString(stamp);
byte[] deserialize = System.Text.Encoding.UTF8.GetBytes(serialize);
//deserialize == byte[]{0,0,0,0,0,1,239,191,189,115}
Run Code Online (Sandbox Code Playgroud)
为什么是邮票!=反序列化?
我有一组对象,并希望修改该集合中对象的属性.如果我有一个对象,我的ChangeStuff方法工作正常,从方法返回时修改对象.(Main的前4行)
然而,当迭代整个集合时,我想我错过了一些东西,因为我的更改值似乎停留在foreach循环的范围内.
我不应该通过ref(或使用out参数)传递对象,因为我没有返回新值.
对于大代码块很抱歉,但它简化了我可以做到并且仍然可以证明我的问题.
class foobar
{
public string string1;
public string string2;
}
static void Main(string[] args)
{
/***** THIS WORKS!! *****/
foobar singleFb = new foobar { string1 = "foo2", string2 = "bar2" };
ChangeStuff(singleFb);
Console.WriteLine(singleFb.string1 + ", " + singleFb.string2);
Console.ReadLine(); //pause to read output
/***** THIS DOESN'T WORK!! *****/
List<foobar> myList = new List<foobar> { new foobar {string1 = "foo1", string2 = "bar1"}, new foobar {string1 = "foo2", string2 = "bar2"}, new …Run Code Online (Sandbox Code Playgroud)