编译C#代码时出错 [英] error compiling c# code

查看:140
本文介绍了编译C#代码时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace SilverlightTest.Web
{
    public class wcfSpecialLoansImplementation
    {
        SpecialLoansDataContext  db = new SpecialLoansDataContext();
        object specialLoans = from specialLoan in db.SPECIAL_LOAN select specialLoan;
    }
}


这是我用来尝试使用linq从我的名为SpecialLoans的sqlserver表中获取数据的代码.如示例所示,我已经创建了一个名为SpecialLoans.dbml的linq实体类.代码无法编译,这导致db.SPECIAL_LOAN节出现问题.它说:字段初始化程序无法引用非静态字段,方法或属性SilverlightTest.Web.wcfSpecialLoansImplementation.db''
我在做什么错?


this is the code i use to try to use linq to get data from my sqlserver table called SpecialLoans. I have created a linq entity class called SpecialLoans.dbml as seen in an example. the code wont compile cause something seem to be wrong with the section db.SPECIAL_LOAN. it says : A field initializer cannot reference the non-static field, method, or property SilverlightTest.Web.wcfSpecialLoansImplementation.db''
what am i doing wrong?

推荐答案

尝试将dbspecialLoans的初始化放入构造函数中.
Try putting the initialization of db and specialLoans into the constructor.

public class wcfSpecialLoansImplementation
{
    SpecialLoansDataContext  db;
    object specialLoans;

    public wcfSpecialLoansImplementation
    {
        db = new SpecialLoansDataContext();
        specialLoans = from specialLoan in db.SPECIAL_LOAN select specialLoan;
    }
}


这篇关于编译C#代码时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆