我会首先展示一些代码,然后我会试着说出我想要的东西。
Thread thread = db.Threads
.AsNoTracking()
.FirstOrDefault(x=>x.Name == name);
var threadTopics = db.Topics
.AsNoTracking()
.Where(x => x.ThreadId == thread.Id && x.PinType != PinType.Advertisement)
.ToList();
thread.Topics = threadTopics;
基本上,我希望我的主题有主题,但我不需要所有主题,我只需要"不需要广告"话题。这对我有用,但我觉得我可以用一个请求做到这一点,但我不知道如何。
我尝试了这个,但它无法正常工作:
Thread thread = db.Threads
.AsNoTracking()
.Where(x=>x.Name == name)
.Select(x=> new Thread
{
Topics = x.Topics.Select(x=>x.PinType != PinType.Advertisement)
});
型号:
class Thread {
int ThreadId {get;set;}
ICollection<Topic> Topics {get;set;}
}
class Topic {
int TopicId {get;set;}
int ThreadId {get;set;}
Thread Thread {get;set;}
PinType PinType {get;set;}
}
答案 0 :(得分:0)
Thread thread = db.Threads
.AsNoTracking()
.Where(x=>x.Name == name)
.Select(x=> new Thread
{
Topics = x.Topics.Select(x=>x.PinType != PinType.Advertisement)
});
左侧是Thread,右侧是IEnumberable。你也错过了新主题中的Id。 将其更改为:
var threads = db.Threads
.AsNoTracking()
.Where(x=>x.Name == name)
.Select(x=> new Thread
{
ThreadId = x.ThreadId,
Topics = x.Topics.Where(x=>x.PinType != PinType.Advertisement).ToList()
});
也许你需要
Thread thread = threads.FirstOrDefault();
然后我们可以将其简化为
Thread thread = db.Threads
.AsNoTracking()
.Where(x=>x.Name == name)
.Select(x=> new Thread
{
ThreadId = x.ThreadId,
Topics = x.Topics.Where(x=>x.PinType != PinType.Advertisement).ToList()
}).FirstOrDefault();
答案 1 :(得分:0)
这就像我想要的那样。首先,我寻找我需要的线程,在我找到它之后 - 我从中删除了一些主题。我想在一个请求中执行此操作,但我认为这也是“正常”。
Thread thread = db.Threads
.AsNoTracking()
.FirstOrDefault(x=>x.Name == name);
thread.Topics = thread.Topics.Where(x => x.PinType != PinType.Advertisement).ToList();