c# - 如何在 asp.net 中查找 SQL 数据库中具有某个 bool 列值为 true 的所有行?

标签 c# mysql asp.net

我有一个包含两个 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/

相关文章:

c# - 如何在不使用触摸屏的情况下模拟触摸事件

javascript - 当输入文本框为空时阻止按钮 asp.Net webforms

c# - WinStore : Slide a Grid on- and off-screen

mysql - 从一列中提取子字符串并将其放入另一个 SQL

mysql - 生成 MySQL 插入并更改列名称

ASP.NET 身份数据库第一个自定义用户和角色

asp.net - 保护文件夹免受外部请求

c# - 等待操作超时mvc

PHP 和 MySQL - 使用数组填充选择下拉列表

c# - 设置 SmartNavigation=True 会导致 javascript offset.Width/offset.Height 出错