小编Yul*_*dra的帖子

如何使用async和await正确处理

我正在努力使代码替换ThreadTask.睡眠/延迟只是代表长时间运行的活动.

static void Main(string[] args)
{
    ThreadDoWork();
    TaskDoWork();
}
public static void ThreadDoWork()
{
    using (var dispose = new ThreadDispose())
    {
        dispose.RunAsync();
    }
}
public static async void TaskDoWork()
{
    using (var dispose = new TaskDispose())
    {
        await dispose.RunAsync();
    }
}
public class ThreadDispose : IDisposable
{
    public void RunAsync ()
    {
        ThreadPool.QueueUserWorkItem(state =>
        {
            Thread.Sleep(3000);
        });
    }
    void IDisposable.Dispose()
    {
        File.AppendAllText("D:\\test.txt", "thread disposing");
    }
}
public class TaskDispose : IDisposable
{
    public async Task RunAsync() …
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading dispose async-await

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

实现IEnumerable和IEnumerator的原因是什么

我正在准备我的C#EXAM.我对这个问题的答案感到困惑:

程序可以使用IEnumerableIEnumerator接口来执行以下哪项操作?

一个.使用MoveNext和Reset来移动对象列表.
湾 使用foreach移动对象列表.
C.按索引移动对象列表.
d.使用yield return语句创建迭代对象列表.

我的回答是b).但是这本书:MCSD认证工具包说它是a).

有人可以告诉我为什么吗?我意识到你可以获得Enumerator使用GetEnumerator()然后调用MoveNextReset方法来遍历列表(并用于Current访问迭代器引用的当前元素).但是没有实现IEnumerableIEnumeratorforeach循环中使用对象的原因?

c#

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

在LINQ中返回null而不是默认值

我有LINQ查询,它必须检索一些DateTime值.Somethimes我没有匹配,我必须为该DateTime值返回NULL而不是DateTime的默认值.

我怎样才能避免这种情况并返回NULL而不是默认值?

我的LINQ:

CreatedDate = ctaMatch.Select(d => d.CreatedDate).DefaultIfEmpty().FirstOrDefault()
Run Code Online (Sandbox Code Playgroud)

在DefaultIfEmpty中,我只能放置DateTime.

c# linq

11
推荐指数
2
解决办法
7979
查看次数

如何在阵列初始化时自动编码?

每次我有数组初始化并尝试通过按CTRL+K和格式化代码CTRL+D,代码缩进不会自动格式化.

示例代码.

var users = new[]
{
    new User(),
    new User (     ),
       new User { Id = 1 },
    new User {     Id = 1 }   ,
            new     User { Id = 1 }   ,
    new    User { Id = 1      },
};
Run Code Online (Sandbox Code Playgroud)

预期结果.

var users = new[]
{
    new User(),
    new User(),
    new User { Id = 1 },
    new User { Id = 1 },
    new User { Id = 1 },
    new …
Run Code Online (Sandbox Code Playgroud)

c# code-formatting indentation visual-studio-2012

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

System.DateTime类型不是受支持的类型.更改为使用System.DateTimeOffset

我们正在创建WebApi 2.2服务,并且正在使用上面列出的技术.我们的后端数据存储区是MySql 5.6.我们使用dotConnect for MySql来处理数据存储.在数据库中,有一个RowVersion列,其类型为Timestamp.在EF中,我成功生成了模型,但我注意到RowVersion设置为DateTime.当我运行WebApi时,我得到以下运行时异常,因此我需要将类型更改为DateTimeOffset,因为Timestamp不可用.

在我们的应用程序中,我们将使用带有ETag的RowVersion进行并发处理.因此,我们只会在我们的应用程序中阅读RowVersion; 每当插入或更新发生时,数据库将自动更新RowVersion.

我不知道如何纠正这个问题...也许,有一些方法可以添加一个自动类型转换,因此模型中的RowVersion是一个Int64,我们通过发送Timestamp.value自动在Int64和Timestamp之间进行转换应用.我们只是阅读它,所以这似乎是合理的.

当我在EF模型中将RowVersion更改为Int64并构建应用程序时,我收到以下错误:

错误1错误2019:指定的成员映射无效.'Model.customer'类型中成员'Version'的类型'Edm.Int64 [Nullable = True,DefaultValue =]'与'Devart.Data.MySql.timestamp不兼容[Nullable = True,DefaultValue =,Precision = 0 ''类型'Model.Store.customers'中的成员'Version'.C:\ PROJECTS\ServiceMySql\ServiceMySql\Models\Model.edmx 898 17 ServiceMySql

我非常感谢您帮助弄清楚如何解决此问题.

感谢您的时间和建议,

麦克风

本帖子开头提到的例外情况:

System.ArgumentException未由用户代码处理
HResult = -2147024809 Message =类型'System.Nullable 1 configurationCallback)位于c:\ PROJECTS\XXXXServiceMySql\XXXXServiceMySql\Global.asax.cs中的XXXXServiceMySql.WebApiApplication.Application_Start():第17行 InnerException :1[[System.DateTime, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' of property 'Version' in the 'XXXXServiceMySql.Models.customer' type is not a supported type. Change to use 'System.DateTimeOffset' or ignore this type by calling Ignore<XXXXServiceMySql.Models.customer>() on 'System.Web.OData.Builder.ODataModelBuilder'. Parameter name: navigationProperty Source=System.Web.OData
ParamName=navigationProperty StackTrace: at System.Web.OData.Builder.EntityTypeConfiguration.AddNavigationProperty(PropertyInfo navigationProperty, EdmMultiplicity multiplicity, …

mysql dotconnect odata asp.net-web-api entity-framework-6

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

有没有选择器参数和jquery委托的jquery有什么区别?

我正在使用jquery 1.10.我想知道这三个功能之间的区别.

哪个功能更好,为什么?

委托功能的目的是什么?

$(".dropdown-menu").on("click", ".show_opt_menu", function() {
    alert("hello");
});
$(".dropdown-menu .show_opt_menu").on("click", function() {
    alert("hello");
});
$(".dropdown-menu").delegate(".show_opt_menu", "click", function() {
    alert("Delegate");
});
Run Code Online (Sandbox Code Playgroud)

有人可以解释一下吗?

javascript jquery

6
推荐指数
2
解决办法
343
查看次数

实体框架 6.1.1 索引的命名约定

我了解如何将约定添加到代码优先(通过迁移)项目。我已经成功地执行了表名称,甚至将 GUID Id 字段更改为非聚集。

但我还没有找到如何在未给出名称时更改 EF 提供的默认索引名称。

[Index(IsUnique = true)]
public string Code { get; set; }

[Index]
public string Description { get; set; }
Run Code Online (Sandbox Code Playgroud)

我有这两个要求。顶部索引应命名为UX_[schema]_[table]_Code,第二个索引应命名为IX_[schema]_[table]_Description

我还需要支持多列索引,其中 IsUnique 仍然是 UX,但列部分是所有列的驼峰式组合(例如, UX_[schema]_[table]_CodeDescription如果上面的两列位于同一索引中)。

我假设我需要在 IndexAttributeConvention 之后添加它,以便所有当前功能都可以工作并创建 IndexAnnotations。但如果属性(或 Fluent 构造)中留空,我无法找到索引在哪里接收其初始名称。

提前致谢。

entity-framework naming-conventions ef-code-first entity-framework-6.1

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

如何将导航属性名称更改为有意义的名称

我正在使用Database First和Entity Framework 5.我们有两个表(大规模简化):

  • 地址
    • 镇(等)
  • 顾客
    • 名称
    • 帐单地址
    • 邮寄地址
    • AltDeliveryAddress

当我们使用Visual Studio将数据库导入EF("从数据库更新模型")时,我们最终得到如下代码:

Customer myCustomer;
var a = myCustomer.Address;
var b = myCustomer.Address1;
var c = myCustomer.Address2;
Run Code Online (Sandbox Code Playgroud)

我想要的显然是这样的:

var a = myCustomer.BillingAddress;
var z = myCustomer.BillingAddress.Street; // etc.
Run Code Online (Sandbox Code Playgroud)

我可以在设计器中编辑模型,更改导航属性以给我正确的名称.但是,这不是一个可行的解决方案,因为我们每次更改数据库时都会重建模型.

我尝试过的一个选项是创建一个像这样的部分类(从现有的MyModel.Designer.cs复制的代码,只更改了属性名):

public partial class Customer : EntityObject
{
    [EdmRelationshipNavigationPropertyAttribute("MyModel", "FK_Customers_Addresses_BillingAddress", "Address")]
    public Address BillingAddress
    {
        get {
            return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<LookupItem>("MyModel.FK_Customers_Addresses_BillingAddress", "Address").Value;
        }
        set {
            ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<LookupItem>("MyModel.FK_Customers_Addresses_BillingAddress", "Address").Value = value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我运行它时,我收到以下错误:

概念类型"MyModel.Customer"中的成员数与对象端类型"MyNamespace.DataModel.Customer"上的成员数不匹配.确保成员数量相同.

我已经尝试使用[NotMapped()]属性,但这没有任何区别.如果我删除[EdmRelationshipNavigationPropertyAttribute ...]属性,那么Linq会抱怨以下错误:

LINQ to Entities不支持指定的类型成员'BillingAddress'.仅支持初始值设定项,实体成员和实体导航属性.

有没有其他方法可以在Customers对象中实现有意义的名称?

这就是我最终想要的东西:

var …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework ef-database-first entity-framework-5

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

错误CS0103:当前上下文中不存在名称""

当我的视图加载时,我需要检查用户正在访问哪个域,并根据结果,为页面上显示的徽标引用不同的样式表和图像源.

这是我的代码:

@{
    string currentstore=HttpContext.Current.Request.ServerVariables["HTTP_HOST"];

    if (currentstore == "www.mydomain.com")
    {
        <link href="/path/to/my/stylesheets/styles1-print.css" rel="stylesheet" type="text/css" />
        string imgsrc="/content/images/uploaded/store1_logo.jpg";
    }
    else
    {
        <link href="/path/to/my/stylesheets/styles2-print.css" rel="stylesheet" type="text/css" />
        string imgsrc="/content/images/uploaded/store2_logo.gif";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,再往前一点我调用imgsrc变量如下:

<a href="@Url.RouteUrl("HomePage")" class="logo"><img  alt="" src="@imgsrc"></a>
Run Code Online (Sandbox Code Playgroud)

我收到一个错误说:

错误CS0103:当前上下文中不存在名称"imgsrc"

我想这是因为"imgsrc"变量是在一个现在关闭的代码块中定义的......?

在页面下方引用此变量的正确方法是什么?

c# asp.net-mvc codeblocks razor

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

如何在WPF数据网格中一起选中所有复选框

我的wpf数据网格是,

<dg:DataGrid.Columns >
    <dg:DataGridTemplateColumn>
        <dg:DataGridTemplateColumn.Header>
            <CheckBox Content=" Slect All" Click="CheckBox_Click" />
        </dg:DataGridTemplateColumn.Header>
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <CheckBox Name="chkSelectAll"   Margin="45 2 0 0" Click="chkSelectAll_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn Header="Edit Row"  >
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Edit" Click="Button_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
    <dg:DataGridTemplateColumn Header="Delete Row">
        <dg:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Delete" Click="DeleteButton_Click" />
            </DataTemplate>
        </dg:DataGridTemplateColumn.CellTemplate>
    </dg:DataGridTemplateColumn>
</dg:DataGrid.Columns>
Run Code Online (Sandbox Code Playgroud)

单击此选择全部复选框我需要将所有复选框选中/取消选中.任何人都可以帮助我完成它.还有一件事,Dynamicaly我将数据填充到datgrid.

- 谢谢

c# wpf wpfdatagrid

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