小编hor*_*rgh的帖子

如何检查泛型方法的类型T是否为IEnumerable <>并循环它?

我想做这样的事情

void DoSomething<T>(T param)
{
    if param is IEnumerable<?>
    {
        loop param and do stuff
    }
}
Run Code Online (Sandbox Code Playgroud)

我不知道在问号的位置做什么.它有可能吗?

c# generics

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

StatusStrip 可以根据其项目的大小自动更改其高度吗?

我有一个包含许多项目的状态条。其中之一是ToolStripStatusLabelwith Spring = True。当标签的文字太长时,人们就看不清它。

是否可以使状态条变高并以多行显示整个文本?

c# statusstrip autosize winforms

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

为什么我得到"数据库'tempdb的日志文件'已满"

让我们有一个支付表,其中35列包含主键(autoinc bigint)和3个非聚集的非唯一indeces(每个在一个int列上).

在表的列中,我们有两个日期时间字段:

  1. 付款日期 datetime NOT NULL

  2. EDIT_DATE datetime NULL

该表有大约1 200 000行.只有~1000行有edit_date column = null.9000行的edit_date不为null且不等于payment_date其他行有edit_date = payment_date

当我们运行以下查询1时:

select top 1 *
from payments
where edit_date is not null and (payment_date=edit_date or payment_date<>edit_date)
order by payment_date desc
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

服务器需要几秒钟才能完成.但是如果我们运行查询2:

select top 1 *
from payments
where edit_date is not null
order by payment_date desc
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

执行结束于数据库'tempdb'的日志文件已满.备份数据库的事务日志以释放一些日志空间.

如果我们用某些列替换*,请参阅查询3

select top 1 payment_date
from payments
where edit_date is not null
order by payment_date desc
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

它也会在几秒钟内完成.

魔术在哪里?

编辑 …

sql sql-server sql-server-2000 sql-execution-plan

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

在ASP.Net MVC 3项目中无法使用Entity Framework进行SaveChanges

学习asp.net mvc 3 + EF代码优先.我是两个新手.我的例子是微不足道的,但我仍然无法使其发挥作用.缺少一些简单而明显的东西......

我上课了:

 public class Product
 {
    [HiddenInput(DisplayValue = false)]
    public int ProductID { get; set; }

    [Required(ErrorMessage = "Please enter a product name")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please enter a description")]
    [DataType(DataType.MultilineText)]
    public string Description { get; set; }

    [Required]
    [Range(0.01, double.MaxValue, ErrorMessage = "Please enter a positive price")]
    public decimal Price { get; set; }

    [Required(ErrorMessage = "Please specify a category")]
    public string Category { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和a …

c# entity-framework savechanges ef-code-first asp.net-mvc-3

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

在 LINQ 中对 IEnumerable 进行排序

如何对给定的示例进行排序。

        IEnumerable<extra> eList = new List<extra>()
        {
            new extra{ id = 1, text = "a"},
            new extra{ id = 2, text = "g"},
            new extra{ id = 3, text = "i"},
            new extra{ id = 4, text = "e"},
            new extra{ id = 5, text = "f"},
            new extra{ id = 6, text = "d"},
            new extra{ id = 7, text = "c"},
            new extra{ id = 8, text = "h"},
            new extra{ id = 9, text = "b"}
        }; …
Run Code Online (Sandbox Code Playgroud)

c# linq linq-to-objects

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

从通用列表底部删除重复项

我试图从通用列表的底部删除重复项.我的课程定义如下

public class Identifier
{
    public string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我已经定义了另一个实现IEqualityComparer的类来从List中删除重复项

public class DistinctIdentifierComparer : IEqualityComparer<Identifier>
{
    public bool Equals(Identifier x, Identifier y)
    {
        return x.Name == y.Name;
    }

    public int GetHashCode(Identifier obj)
    {
        return obj.Name.GetHashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,我正在尝试删除旧项目并保持最新状态.例如,如果我有如下定义的标识符列表

Identifier idn1 = new Identifier { Name = "X" };
Identifier idn2 = new Identifier { Name = "Y" };
Identifier idn3 = new Identifier { Name = "Z" };
Identifier idn4 = new Identifier { Name = …
Run Code Online (Sandbox Code Playgroud)

c# linq generics

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

如何修剪字符串中的任何字符(或子字符串)?

我基本上使用C#.我可以做:

string trimmed = str.Trim('\t');
Run Code Online (Sandbox Code Playgroud)

修剪字符串中的列表str并将结果返回到trimmed.

在delphi7中,我发现只有Trim修剪空间.

我怎样才能实现相同的功能?

delphi trim delphi-7

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

如何替换一些保留他人的细分价值?

要继续我之前的一个问题(生成传出URL时选择了意外的路由),请考虑以下路由(没有默认值,没有约束):

"{controller}/{month}-{year}/{action}/{user}"
Run Code Online (Sandbox Code Playgroud)

假设在通过匹配此路由的传入URL呈现某些页面时(因此请求上下文包含每个路径段的值),我需要生成仅更改的URL,monthyear保留所有其他段的确切值.

根据规则,我在链接的问题中提到过(即,路由系统将仅重用URL模式中早先出现的段变量的值,而不是任何提供的参数.),如果我指定new month并且year只通过匿名对象,我将失去该user段的价值(即路线甚至不会匹配).

我知道有两种方法可以解决这个问题:

  1. 根本不指定匿名对象; 但是在context.RouteData.Values;中设置必要的段值; 在生成所需的url之后,返回原始值,以便使用原始请求段值执行其余页面呈现; 这看起来像这样:

    public static MvcHtmlString GetDateUrl(this RequestContext context, 
                                           DateTime date)
    {
        UrlHelper url = new UrlHelper(context);
    
        object month = null, year = null;
        if (context.RouteData.Values.ContainsKey("month"))
        {
            month = context.RouteData.Values["month"];
            year = context.RouteData.Values["year"];
        }
    
        try
        {
            context.RouteData.Values["month"] = date.Month;
            context.RouteData.Values["year"] = date.Year;
            return MvcHtmlString.Create(
                url.Action((string)context.RouteData.Values["action"]));
        }
        finally
        {
            if (month == null)
            {
                if (context.RouteData.Values.ContainsKey("month"))
                {
                    context.RouteData.Values.Remove("month");
                    context.RouteData.Values.Remove("year"); …
    Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-3

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

TextBoxFor 助手在日期中混合日和月

请考虑以下示例:

  1. 视图模型

    public class FooViewModel
    {
        public DateTime Date { get; set; }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 控制器

    public class HomeController : Controller
    {
        [HttpGet]
        public ActionResult Index(FooViewModel foo)
        {
            return View(foo);
        }
        [HttpPost]
        public ActionResult Submit(FooViewModel foo)
        {
            return RedirectToAction("Index", foo);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 看法

    @model MvcApplication1.Models.FooViewModel
    <h1>@Model.Date</h1>
    @using (Html.BeginForm("Submit", "Home", FormMethod.Post))
    {
        @Html.TextBoxFor(m => m.Date)
        <input type="submit" value"Submit" />
    }
    
    Run Code Online (Sandbox Code Playgroud)
  4. 路线

    routes.MapRoute(
        null,
        "",
        new { controller = "Home", action = "Index" }
    );
    routes.MapRoute(
        null,
        "{action}",
        new { controller = "Home" …
    Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc-3

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

如何防止方法跨多个线程运行?

我正在开发一个Web应用程序,其中有几个用户可以更新相同的记录.因此,如果用户同时更新同一记录,以避免出现问题,我将其更改保存在队列中.当每次保存发生时,我想调用一个在另一个线程上处理队列的方法,但我需要确保该方法不能在另一个线程中再次调用时运行.我已经阅读了几篇关于这个主题的帖子,但不确定什么对我的情况最好.以下是我现在的代码.这是处理它的正确方法吗?

public static class Queue {
    static volatile bool isProcessing;
    static volatile object locker = new Object();

    public static void Process() {
        lock (locker) {
            if (!isProcessing) {
                isProcessing = true;

                //Process Queue...

                isProcessing = false;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# multithreading

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