Я нашел этот вопрос , который предполагал, что я смогу достичь своей цели.Я хочу возвращать только сообщения, в которых поле parent_post равно нулю.
Вот как мои данные выглядят в моей таблице, из которой они извлекаются:
create table dbo.post
(post_id int not null primary key,
thread_id int not null,
parent_post int,
creator int not null,
post_text nvarchar(max) not null)
insert into dbo.post (post_id, thread_id, parent_post, creator, post_text) values
(1,1,null,123,'Here is the first post in the forum!'),
(2,1,null,123,'Here is the second post!'),
(3,1,2,123,'Here is a reply! Let''s hope it works!'),
(4,1,2,123,'Here is a second reply. I wonder how this looks?'),
(5,1,3,123,'Here is a third level deep reply. ')
В моем контроллере для моего API это выглядиткак это:
[HttpGet]
[Route("api/threads/{thread_id}")]
[AcceptVerbs("GET")]
public IEnumerable<post> GetPostsByThread(long thread_id)
{
return db.post.Where(post => post.thread_id == thread_id && post.parent_post == null).ToList();
}
(я избавился от части из связанного вопроса, который использовал «Обрезать», потому что мои пробелы никогда не будут пустым пространством)
Теперь я ожидалчто это вернет записи для постов 1 и 2. Однако, когда я вызываю API, он ничего не возвращает.Когда я перехожу на http://localhost: ##### / api / threads / 1 (или любое другое число), он возвращает это:
<ArrayOfpost xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Test_Forum.Models"/>
Отредактировано, чтобы добавить модель дляПост
namespace Test_Forum.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Spatial;
public partial class post
{
[Key]
[Required]
[Column(Order = 0)]
public long post_id { get; set; }
[Required]
[Column(Order = 1)]
public long thread_id { get; set; }
[Required]
[Column(Order = 2)]
public long? parent_post { get; set; }
[Required]
[Column(Order = 3)]
public long creator { get; set; }
[Required]
[Column(Order = 4)]
public DateTime create_date { get; set; }
[Required]
[Column(Order = 5)]
public DateTime update_date { get; set; }
[Required]
[Column(Order = 6)]
public string post_text { get; set; }
}
}