我想做一行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)
单线有可能吗?
从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)