小编Nag*_*far的帖子

带有2个动作的单行if语句

我想做一行if语句有多于1个动作.

默认是这样的:

(if) ? then : else

userType = (user.Type == 0) ? "Admin" : "User";
Run Code Online (Sandbox Code Playgroud)

但我不需要"别的",我需要一个"别的如果"

像多行中那样:

if (user.Type == 0)
    userType = "Admin" 
else if (user.Type == 1)
    userType = "User"
else if (user.Type == 2)
    userType = "Employee"
Run Code Online (Sandbox Code Playgroud)

单线有可能吗?

c# action if-statement

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

"无法访问已处置的对象"

从linq到sql访问关联对象时遇到问题.我有一篇文章和用户.每篇文章都有一个卖家(用户),每个用户都有很多文章.我通过协会解决了这个问题.

这是我的linq到sql类的样子: linq到sql类

这是协会:

协会

以下是Article.Seller背后的代码:

[global::System.Data.Linq.Mapping.AssociationAttribute(Name="User_Article", Storage="_Seller", ThisKey="SellerID", OtherKey="ID", IsForeignKey=true)]
public User Seller
{
    get
    {
        return this._Seller.Entity;
    }
    set
    {
                   ...
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,当我想要获得文章的卖家时,我收到以下错误:

无法访问已处置的对象.对象名:'Dispose后访问的DataContext.'.

错误发生在卖家的获取中.

任何想法如何处理这个?

编辑:Heres'使用DataContext的代码:

public static List<Article> Read()
{
    using (uDataContext dbx = new uDataContext())
    {
        return dbx.Article.ToList();
    }
}
Run Code Online (Sandbox Code Playgroud)

该列表使用如下:

List<Article> articles = ArticleDALC.Read();

foreach (Article article in articles)
{
    // Exception appears here!
    User seller = article.Seller;
    ....
}
Run Code Online (Sandbox Code Playgroud)

c# linq-to-sql

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

标签 统计

c# ×2

action ×1

if-statement ×1

linq-to-sql ×1