小编Pau*_*ber的帖子

为什么.ToList()与Cast相比是一个瓶颈?示例代码如下

http://codepaste.net/4xzv9k 是显示C#代码的codepaste链接(很抱歉,我无法让代码看到这里)

我的问题是为什么像下面这样的简单查询比强制转换而不是.ToList扩展要快得多:

    //fast:
        for (int i = 0; i < iterations; i++)
        {
              var queryShortWay1 = productList.OrderBy(a => a.ProductName).Where(a =>
                a.ProductName.Length < 99);

            mySecondProductLst = queryShortWay1 as List<Product>; //fast CAST  


        }

    //slow:

        for (int i = 0; i < iterations; i++)
        {
            var queryLongWay1 = productList.OrderBy(a => a.ProductName).Where(a =>
              a.ProductName.Length < 99);
            MyProductList = queryLongWay1.ToList();  //slow 

        }
Run Code Online (Sandbox Code Playgroud)

你会认为.ToList并不是那么慢,但它至少要一个数量级.有什么原因?

c#

2
推荐指数
1
解决办法
85
查看次数

哪个版本的using语句是正确还是最安全的?

以下'using'语句的版本I和II都有效,但我怀疑第一个版本是否有效,因为Visual Studio 2010中的C#垃圾收集器尚未删除变量"context"(实体框架变量).另一方面,我从一个看似有信誉的来源网上获得了第一个版本,所以我认为它没关系?

版本I:

try
{
    using ( AnEFEntity context = new AnEFEntity()) //note: no curly brackets!
    using (var ts = new System.Transactions.TransactionScope())
    {
        // stuff here that uses variable context
    }
}
catch (Exception ex)
{
}
Run Code Online (Sandbox Code Playgroud)

//以上编译很好并且工作正常 - 但是在范围内是第一个'使用'语句?似乎是这样,但它是可疑的.

版本II:

try
{

    using ( AnEFEntity context = new AnEFEntity())
    { //note a curly bracket used for first ‘using’
        using (var ts = new System.Transactions.TransactionScope())
        {
            // stuff here that uses variable context
        }
    } //note the placement of …
Run Code Online (Sandbox Code Playgroud)

c# using-statement visual-studio

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

SQL数据库专家:这是一个瓶颈吗?

我说不,伙伴说是的.我们都很固执.我们将测试,但我需要一些弹药来支持我的情况,即下面的伪代码不是那么大的瓶颈.我们正在做的是运行两个查询.第一个是针对一个大表(100k记录),下面称为BigTable.然后,我们采取我们发现的(通常是30个左右的记录)并运行这些记录(在一个循环中,参见下面的第二个'foreach')对一个名为SmallTable的小得多的表,通常有大约200条记录.我的合作伙伴希望将在SmallTable中找到的记录组合在一起,以便它们出现在BigTable的记录中,但如果这样做,则会失去灵活性,数据库不再处于正常状态.我的伴侣说他不关心普通形式,并声称以下是瓶颈.帮助我赢得这场战斗!当然,我的后备位置将是"让我们测试并看到",但我现在可以使用一些火力.我们期望每秒10个请求(在这种情况下可能是瓶颈,如果有的话,是微不足道的)或高达1500秒(这可能会破坏我们的应用程序在其他地方!大声笑.但我们是乐观主义者).

平台:Microsoft SQL Server 2008,使用以C#和Linq-to-Entities(EF)编写的Web服务,在远程服务器上运行,我们不拥有,但由GoDaddy等服务器公司在'Per Call'上运行/无国籍基础.在SQL Server中设置为默认值的所有内容(例如,Max Pooling = 100,Load Balance Timeout = 0,Pooling = True).我没有进行任何缓存,因为我已经读过缓存不适用于分布式数据库的地方,我认为这是服务器公司使用的.

我很感激.这是伪代码:

Public  string MyWebMethod()
{

List<Record> myrecords = new List<Records>();

try  //try block 1
{
using (AEntityFramework context = new AEntityFramework())
{
var RecordsReturned =   (from x in context.BigTable
                            //some conditions deleted
            select x);

//do some stuff with these records, typically 30 records returned from BigTable, which has 10000 to 100000 records total

foreach (Record r in RecordsReturned)
{  myrecords.Add(r);} //add these records to …
Run Code Online (Sandbox Code Playgroud)

sql profiling sql-server-2008

0
推荐指数
1
解决办法
325
查看次数

Async/Await与WinForms ProgressBar - WinForms版本

请参阅:http://codepaste.net/djw3cw 代码

如果异步编程的异步/等待很快就会像Linq一样,我认为这个问题是使用WinForms ProgressBar进行Async/Await的非平凡扩展

虽然代码是最佳的,但我会很高兴用代替代码的指针或答案.

问题是:如何使用asynch/await设置进度条.在过去,我成功使用了Dispatcher.

Please see: http://codepaste.net/djw3cw  for the code
What is done: a textbox has any text in it converted to an int, then when
"mybutton1" is clicked, work is done based on the int, for int ms (int = 
milliseconds).
During this time, a progressbar "myProgressBar" is shown for 
every tenth-percent step
When work is complete, the label/textblock controls are updated
But the below does not work right: the …
Run Code Online (Sandbox Code Playgroud)

c# asynchronous async-await progress-bar

0
推荐指数
1
解决办法
3788
查看次数