小编meh*_*nik的帖子

C#'dynamic'无法访问另一个程序集中声明的匿名类型的属性

只要我在班级和班级ClassSameAssembly相同的班级中,下面的代码就能正常运行Program.但是当我将类移动ClassSameAssembly到一个单独的程序集时,RuntimeBinderException会抛出一个(见下文).有可能解决它吗?

using System;

namespace ConsoleApplication2
{
    public static class ClassSameAssembly
    {
        public static dynamic GetValues()
        {
            return new
            {
                Name = "Michael", Age = 20
            };
        }
    }

    internal class Program
    {
        private static void Main(string[] args)
        {
            var d = ClassSameAssembly.GetValues();
            Console.WriteLine("{0} is {1} years old", d.Name, d.Age);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:'object'不包含'Name'的定义

at CallSite.Target(Closure , CallSite , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
at ConsoleApplication2.Program.Main(String[] args) in C:\temp\Projects\ConsoleApplication2\ConsoleApplication2\Program.cs:line …
Run Code Online (Sandbox Code Playgroud)

dynamic anonymous-types c#-4.0

85
推荐指数
3
解决办法
3万
查看次数

实体框架代码优先 - 两个具有相同名称但位于不同名称空间的实体

我在以下场景中遇到了db生成问题:

1.cs First.Entities命名空间中的项目实体maped到First_Project表.

namespace First.Entities
{
    #region using section

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity.ModelConfiguration;
    using System.Diagnostics.CodeAnalysis;

    #endregion

    [Table("First_Project")]
    public class Project
    {
        [Key]
        public int Id
        {
            get;
            set;
        }

        [Required]
        [MaxLength(1000)]
        public string Name
        {
            get;
            set;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2.cs在Second.Entities命名空间中的项目实体maped到Second_Project表.

namespace Second.Entities
{
    #region using section

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Data.Entity.ModelConfiguration;
    using System.Diagnostics.CodeAnalysis;

    #endregion

    [Table("Second_Project")]
    public class Project
    {
        [Key]
        public int Id
        {
            get;
            set;
        }

        [Required]
        [MaxLength(1000)]
        public string Name
        {
            get;
            set;
        } …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework code-first entity-framework-4.1

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

Parallel EntityFramework

以下示例是否可以与实体框架并行工作?

using (var dbContext = new DB())
{
var res = (from c in dbContext.Customers
           orderby c.Name
           select new
                    {
                    c.Id, 
                    c.Name,
                    c.Role
                    }
          ).ToDictionary(c => c.Id,
                         c => new Dictionary<string, object> {
                                                               { "Name",c.Name },
                                                               { "Role", c.Role }
                                                             });
}
Run Code Online (Sandbox Code Playgroud)

例如,如果我添加AsParrallel,将会改变什么?

using (var dbContext = new DB())
{
var res = (from c in dbContext.Customers
           orderby c.Name
           select new
                    {
                    c.Id, 
                    c.Name,
                    c.Role
                    }
          ).AsParallel().ToDictionary(c => c.Id,
                         c => new Dictionary<string, object> {
                                                               { "Name",c.Name },
                                                               { "Role", …
Run Code Online (Sandbox Code Playgroud)

c# parallel-processing entity-framework

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