【问题标题】:Super-simple linq query still doesn't work超级简单的 linq 查询仍然不起作用
【发布时间】:2016-07-21 22:37:54
【问题描述】:

尝试使用 Linq 和 Entity Framework 制作一个超级简单的列表数据库。查询运行,但没有返回我输入的数据:

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();
        }
    }
}

我已尝试尽可能简化,但似乎无法显示结果。我的错误是什么?

【问题讨论】:

    标签: c# entity-framework linq


    【解决方案1】:

    在查询之前尝试保存更改。

    db.SaveChanges();
    

    【讨论】:

    • 呜呜呜。不敢相信我忽略了这一点。谢谢!
    猜你喜欢
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多