未处理的异常:System.InvalidOperationException:“ConsoleApplication3.Purchase”的成员访问“十进制价格”在类型上不合法,依此类推

时间:2013-09-20 11:48:09

标签: c# sql-server linq linq-to-sql

这是完整的代码

[Table(Name = "Customer")]
    public class Customer
    {
        [Column(IsPrimaryKey = true, Name = "ID")]
        public int ID { get; set; }

        [Column(Name = "Name")]
        public string Name { get; set; }


        [Association(Name = "Customer_Purchases", ThisKey = "ID", OtherKey = "CustomerID")]
        public EntitySet<Purchase> PurchaseList { get; set; }

        public List<Purchase> Purchases
        {
            get
            {
                return new List<Purchase>(PurchaseList.AsEnumerable());
            }
        }

    }

[Table(Name = "Purchase")]
public class Purchase
{
    [Column(IsPrimaryKey = true, Name = "ID")]
    public int ID { get; set; }

    [Column(Name = "CustomerID")]
    public int CustomerID { get; set; }

    [Column(Name = "Descriptions")]
    public string Descriptions { get; set; }

    [Column(Name = "Price")]
    public decimal Price { get; set; }

    private EntityRef<Customer> _Customer;
    [Association(Storage = "_Customer", ThisKey = "CustomerID", IsForeignKey = true)]
    public Customer Customer
    {
        get { return this._Customer.Entity; }
        set { this._Customer.Entity = value; }
    }
}

class NutshellContext : DataContext 
{
    public NutshellContext(string connectionString)
        : base(connectionString)
    {
    }
    public Table<Customer> Customers
    {
        get { return GetTable<Customer>(); }
    }

    public Table<Purchase> Purchases
    {
        get { return GetTable<Purchase>(); }
    }

}

主要功能

var context = new NutshellContext(@"Server=.\SQLEXPRESS;Database=master;Trusted_Connection=True;");
        var query = from c in context.Customers
                    select
                    from p in c.Purchases
                    select new { c.Name, p.Price };
        foreach (var customerPurchaseResults in query)
            foreach (var namePrice in customerPurchaseResults)
                Console.WriteLine(namePrice.Name + " spent " + namePrice.Price);

我收到此错误:未处理的异常:System.InvalidOperationException:“ConsoleApplication3.Purchase”的成员访问“十进制价格”在类型上不合法,依此类推。 我该如何解决这个问题?

基本上sql数据参数与我几周前在这个帖子Relating More than two tables through foreign keys中提出的问题是一样的,这对我来说是有益的,这要归功于回答者。

所以我修改了代码并在main函数上放了一些东西。运行代码似乎得到一个错误。顺便说一句,我所关注的书是“C#简而言之5.0”,主题是关于协议 - 使用Linq到Sql的执行执行。

这是完整的代码

[Table(Name = "Customer")]
    public class Customer
    {
        [Column(IsPrimaryKey = true, Name = "ID")]
        public int ID { get; set; }

        [Column(Name = "Name")]
        public string Name { get; set; }


        [Association(Name = "Customer_Purchases", ThisKey = "ID", OtherKey = "CustomerID")]
        public EntitySet<Purchase> PurchaseList { get; set; }

        public List<Purchase> Purchases
        {
            get
            {
                return new List<Purchase>(PurchaseList.AsEnumerable());
            }
        }

    }

[Table(Name = "Purchase")]
public class Purchase
{
    [Column(IsPrimaryKey = true, Name = "ID")]
    public int ID { get; set; }

    [Column(Name = "CustomerID")]
    public int CustomerID { get; set; }

    [Column(Name = "Descriptions")]
    public string Descriptions { get; set; }

    [Column(Name = "Price")]
    public decimal Price { get; set; }

    private EntityRef<Customer> _Customer;
    [Association(Storage = "_Customer", ThisKey = "CustomerID", IsForeignKey = true)]
    public Customer Customer
    {
        get { return this._Customer.Entity; }
        set { this._Customer.Entity = value; }
    }
}

class NutshellContext : DataContext 
{
    public NutshellContext(string connectionString)
        : base(connectionString)
    {
    }
    public Table<Customer> Customers
    {
        get { return GetTable<Customer>(); }
    }

    public Table<Purchase> Purchases
    {
        get { return GetTable<Purchase>(); }
    }

}

主要功能

var context = new NutshellContext(@"Server=.\SQLEXPRESS;Database=master;Trusted_Connection=True;");
        var query = from c in context.Customers
                    select
                    from p in c.Purchases
                    select new { c.Name, p.Price };
        foreach (var customerPurchaseResults in query)
            foreach (var namePrice in customerPurchaseResults)
                Console.WriteLine(namePrice.Name + " spent " + namePrice.Price);

编辑: 这是输出控制台上显示的完整错误:

Unhandled Exception: System.InvalidOperationException: Member access 'System.Decimal Price' of        'ConsoleApplication3.Purchase' not legal on type        'System.Collections.Generic.List`1[ConsoleApplication3.Purchase].
   at System.Data.Linq.SqlClient.SqlMember.set_Expression(SqlExpression value)
   at System.Data.Linq.SqlClient.SqlFactory.Member(SqlExpression expr, MemberInfo member)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.AccessMember(SqlMember m, SqlExpression expo)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitMember(SqlMember m)
   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpressionexpr)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitNew(SqlNew sox)
   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpressionexpr)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select)

   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
   at System.Data.Linq.SqlClient.SqlVisitor.VisitSequence(SqlSelect sel)
   at System.Data.Linq.SqlClient.SqlVisitor.VisitMultiset(SqlSubSelect sms)
   at System.Data.Linq.SqlClient.SqlVisitor.VisitSubSelect(SqlSubSelect ss)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSubSelect(SqlSubSelect ss)
   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select)

   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
   at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitIncludeScope(SqlIncludeScope scope)
   at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node)
   at System.Data.Linq.SqlClient.SqlBinder.Bind(SqlNode node)
   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape,
 Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations)
   at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations     annotations)
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
   at System.Data.Linq.DataQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
   at ConsoleApplication3.Program.Main() in c:\Users\Name-PC\Documents\Visual Studio 2012\Projects\Nutshell\ConsoleApplication3\ConsoleApplication3\Program.cs:line 113

2 个答案:

答案 0 :(得分:1)

如果查看发布的错误堆栈,则表示错误在第113行。

\ConsoleApplication3\ConsoleApplication3\Program.cs:line 113

如果你看一下这行,我打赌你会发现一些像“Price.Name”这样的代码吗?如果是这样,你去吧。

答案 1 :(得分:1)

查看此链接。他们也有同样的问题。

return-list-using-select-new-in-linq