简体   繁体   中英

Super-simple linq query still doesn't work

Trying to use Linq and the Entity Framework to make a super-simple lists database. The query runs, but it does not return the data I enter into it:

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

Here is the actual query:

                var queryresults = from item in db.ListItems
                                   orderby item.Title
                                   select item;

But the foreach loop doesn't print out anything:

                WriteLine("Printing out the list:");
                foreach (var item in queryresults)
                {
                    WriteLine("Item's name:");
                    WriteLine(item.Title);
                }

The rest:

            }

            WriteLine("All done.");
            Console.Read();
        }
    }
}

I've tried to simplify as much as possible, but I cannot seem to get the results to show up. What is my mistake here?

查询之前,请尝试保存更改。

db.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM