我有一个包含两个 bool 列的表:isActive 和 isAcceptingParticipants。
我想获取这两个条件都成立的所有行。
我已包含下面的模型文件以及我想要的服务的部分实现。
using System;
using System.ComponentModel.DataAnnotations;
namespace ProjectTracker.Models
{
public class Project
{
[Key]
public int Id { get; set; }
public int CountParticipants { get; set; }
public int CountActiveParticipants { get; set; }
public Boolean isActive { get; set; }
public Boolean isAcceptingParticipants { get; set; }
public int WhoseTurn { get; set; }
}
}
这是我想要实现 GetInProgress 的服务模块,它将返回 isActive 和 isAccepting 参与者都为 true 的所有行。
using ProjectTracker.Models;
using System.Collections.Generic;
using System;
using ProjectTracker.Data;
using System.Linq;
namespace ProjectTracker.Services
{
public interface IProjectData
{
IEnumerable<Project> GetAcceptingParticipants();
IEnumerable<Project> GetInProgress();
Project ParticipatingIn(int id); //Pass userId to this, returns the project that the user is part of
Project Add(Project newProject);
Project Get(int id);
void Delete(int id);
void Commit();
}
public class SqlProjectData : IProjectData
{
private ApplicationDbContext _context;
public SqlProjectData(ApplicationDbContext context)
{
_context = context;
}
public Project Add(Project newProject)
{
_context.Add(newProject);
Commit();
return newProject;
}
public void Commit()
{
_context.SaveChanges();
}
public void Delete(int id)
{
var toBeDeleted = Get(id);
if (toBeDeleted == null) return;
_context.Remove<Project>(toBeDeleted);
}
public Project Get(int id)
{
return _context.Project.FirstOrDefault(r => r.Id == id);
}
public IEnumerable<Project> GetAcceptingParticipants()
{
throw new NotImplementedException();
}
public IEnumerable<Project> GetInProgress()
{
throw new NotImplementedException();
}
public Project ParticipatingIn(int id)
{
throw new NotImplementedException();
}
}
}
最佳答案
您可以简单地使用 LINQ:
return _context.Project.Where(r => r.isActive && r.isAcceptingParticipants);
关于c# - 如何在 asp.net 中查找 SQL 数据库中具有某个 bool 列值为 true 的所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44210037/