簡體   English   中英

如何在asp.net中查找SQL數據庫中具有某個布爾列值為true的所有行?

[英]How to lookup all rows in an SQL database that have a certain Boolean column value of true in asp.net?

我有一個包含兩個布爾列的表:isActive和isAcceptingParticipants。

我想獲得所有這兩個都是正確的行。

我在下面包括了Models文件,並提供了我想要的服務的部分實現。

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參與者均為真的行。

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);
public IEnumerable<Project> GetInProgress()
{
    return _context.Project.Where(r => r.isActive && r.isAcceptingParticipants).ToList();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM