【问题标题】:How can i find the sum of an attribute in asp.net core如何在 asp.net core 中找到属性的总和
【发布时间】:2021-03-10 15:33:59
【问题描述】:

如何编写控制器方法来获取 AsComponent 类中所有标记属性的总和。我想获得Ascomponents的总分。我想构建一个控制器方法,以便我可以将它与我的反应前端连接。

public class AsComponent
    {
        [Key]
        [ScaffoldColumn(false)]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int AsID { get; set; }
        public string Ascomponent { get; set; }

        [ForeignKey("LOID")]
        public int? Lid { get; set; } 
        public string LOID { get; set; }
        [ForeignKey("POId")]
        public string? POID { get; set; }
        public int Marks { get; set; }
        public string LD { get; set; }
        public string Type { get; set; }
    }

这是我的控制器。它是自动生成的,我已经做了一些编辑。

namespace GroupProject.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AsComponentsController : ControllerBase
    {
        private readonly ObeDbContext _context;

        public AsComponentsController(ObeDbContext context)
        {
            _context = context;
        }

        // GET: api/AsComponents
        [HttpGet]
        public async Task<ActionResult<IEnumerable<AsComponent>>> GetAsComponents()
        {
            return await _context.AsComponents.ToListAsync();
        }

        // GET: api/AsComponents/5
        [HttpGet("{id}")]
        public async Task<ActionResult<AsComponent>> GetAsComponent(int id)
        {
            var asComponent = await _context.AsComponents.FindAsync(id);

            if (asComponent == null)
            {
                return NotFound();
            }

            return asComponent;
        }

        // POST: api/AsComponents
        [HttpPost]
        public async Task<ActionResult<AsComponent>> PostAsComponent(AsComponent asComponent)
        {
            _context.AsComponents.Add(asComponent);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetAsComponent", new { id = asComponent.AsID }, asComponent);
        }

        // DELETE: api/AsComponents/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<AsComponent>> DeleteAsComponent(int id)
        {
            var asComponent = await _context.AsComponents.FindAsync(id);
            if (asComponent == null)
            {
                return NotFound();
            }

            _context.AsComponents.Remove(asComponent);
            await _context.SaveChangesAsync();

            return asComponent;
        }

        private bool AsComponentExists(int id)
        {
            return _context.AsComponents.Any(e => e.AsID == id);
        }
    }
}

【问题讨论】:

    标签: c# asp.net-core asp.net-web-api


    【解决方案1】:

    将 Marks 属性总结在一起就足够了:

    var marksSum = _context.AsCompoments.Sum(component => component.Marks)
    

    【讨论】:

      猜你喜欢
      • 2017-09-20
      • 1970-01-01
      • 2020-01-08
      • 2020-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多