C# 超级简单的linq查询仍然不';行不通
尝试使用Linq和实体框架来创建一个超级简单的列表数据库。查询将运行,但不会返回我输入的数据:C# 超级简单的linq查询仍然不';行不通,c#,entity-framework,linq,C#,Entity Framework,Linq,尝试使用Linq和实体框架来创建一个超级简单的列表数据库。查询将运行,但不会返回我输入的数据: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.Entity; using System.ComponentModel.DataAnnotations; using static Sy
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;
using static System.Console;
namespace CheckList
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Building the list.");
using (var db = new ListContext())
{
ListItem socks = new ListItem() { Title = "Get Socks" };
db.ListItems.Add(socks);
以下是实际查询:
var queryresults = from item in db.ListItems
orderby item.Title
select item;
但是
foreach
循环不会打印任何内容:
WriteLine("Printing out the list:");
foreach (var item in queryresults)
{
WriteLine("Item's name:");
WriteLine(item.Title);
}
其余的:
}
WriteLine("All done.");
Console.Read();
}
}
}
我试图尽可能地简化,但我似乎无法得到结果。我的错误是什么?在查询之前,请尝试保存更改
db.SaveChanges();
Ooooooo。真不敢相信我忽略了这一点。谢谢