我有一个模型,如下所示:
[Table("WebsiteText")]
public class WebsiteTextModel
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string WebsiteSection { get; set; }
[DataType(DataType.MultilineText)]
public string TextToDisplay { get; set; }
}
我有一个 DataContext 类,如下所示:
public class WebsiteTextContext : DbContext
{
public WebsiteTextContext()
: base("DefaultConnection")
{
}
public DbSet<WebsiteTextModel> WebsiteText { get; set; }
}
我想使用 LINQ 为我的主页获取网站文本,并将其填充到 ViewBag 中,所以我正在尝试这个:
public ActionResult Index()
{
var context = new WebsiteTextContext();
var result = from x in context.WebsiteText
where x.WebsiteSection == "Home Page"
select x.TextToDisplay;
ViewBag.Message = result;
return View();
}
但是 var 是 IQueryable,我不知道如何将其转换为字符串。这不起作用:
string result = from x in context.WebsiteText
where x.WebsiteSection == "Home Page"
select x.TextToDisplay;
这不起作用:
var result = from x in context.WebsiteText
where x.WebsiteSection == "Home Page"
select x.TextToDisplay.ToString; //It's still IQueryable<String>
这不起作用:
var result = from x in context.WebsiteText
where x.WebsiteSection == "Home Page"
select x.TextToDisplay.ToArray; //It's now IQueryable<char[]>
我需要做什么?