I have a Model, which is below:
[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; }
}
I have a DataContext class, which is below:
public class WebsiteTextContext : DbContext
{
public WebsiteTextContext()
: base("DefaultConnection")
{
}
public DbSet<WebsiteTextModel> WebsiteText { get; set; }
}
I want to use LINQ to get the website text for my homepage, and stuff it into the ViewBag, so I am trying this:
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();
}
However var is IQueryable, and I can’t figure out how to get it to string. This doesn’t work:
string result = from x in context.WebsiteText
where x.WebsiteSection == "Home Page"
select x.TextToDisplay;
This doesn’t work:
var result = from x in context.WebsiteText
where x.WebsiteSection == "Home Page"
select x.TextToDisplay.ToString; //It's still IQueryable<String>
And this doesn’t work:
var result = from x in context.WebsiteText
where x.WebsiteSection == "Home Page"
select x.TextToDisplay.ToArray; //It's now IQueryable<char[]>
What do I need to do?
You also have
FirstandSingleandSingleOrDefaultLook them all up and pick which version works best for you.