【问题标题】:Compare Min value Entity Framework & ASP.NET MVC比较最小值实体框架和 ASP.NET MVC
【发布时间】:2018-02-18 16:54:16
【问题描述】:

我有很多仓库,每个仓库都有一个交货时间,我如何比较2个仓库并退回交货时间最短的仓库?

比如我得到仓库id 1和id 2,需要比较一下。

我试着写了一个foreach来匹配两个id,但是没有比较,只返回最后一个id运行的deadline。

enter image description here

【问题讨论】:

    标签: asp.net sql-server entity-framework


    【解决方案1】:

    我制作了一个简单的控制台应用程序来向您展示我的方法:

    实体:

    namespace Test
    {
        class Warehouse
        {
            public int Id { get; set; }
            public int DeliveryTime { get; set; }
    
            public Warehouse(int id, int deliveryTime)
            {
                Id = id;
                DeliveryTime = deliveryTime;
            }
        }
    }
    

    服务:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace Test
    {
        class Service
        {
            public IQueryable<Warehouse> Warehouses { get; set; }
            public Service(int quantity)
            {
                var list = new List<Warehouse>();
                var random = new Random();
                for (var i = 0; i < quantity; i++)
                {
                    list.Add((new Warehouse(i, random.Next(1, 100))));
                }
                Warehouses = list.AsQueryable();
            }
    
            public Warehouse GetQuickestWarehouse(int a, int b)
            {
                return Warehouses.Where(w => w.Id == a || w.Id == b).OrderBy(w => w.DeliveryTime).FirstOrDefault();
            }
        }
    }
    

    主要:

    using System;
    using System.Linq;
    
    namespace Test
    {
        class Program
        {
            static void Main(string[] args)
            {
                var service = new Service(100);
                var a = service.Warehouses.FirstOrDefault(w => w.Id == 1);
                var b = service.Warehouses.FirstOrDefault(w => w.Id == 2);
                var min = service.GetQuickestWarehouse(a.Id, b.Id);
    
                Console.WriteLine($"Deliverytime for a: {a.DeliveryTime}");
                Console.WriteLine($"Deliverytime for b: {b.DeliveryTime}");
                Console.WriteLine($"Quickest Warehouse has Id: {min.Id} with delivery time: {min.DeliveryTime}");
                Console.ReadKey();
            }
        }
    }
    

    当然,我的实体模型和我的服务中的构造函数只供控制台应用程序工作,替换为您自己的数据库上下文和逻辑。希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      如果您有仓库 ID,请将它们添加到列表中

      var Ids = new List<int>() { 1, 2 };
      

      然后使用列表过滤 EF 仓库。

      通过 OrderBy 获取最低交货日期

      var lowest = db.Warehouse.Where(w=> Ids.Contains(w.Id)).OrderBy(a => a.DeliveryTime).FirstOrDefault();
      

      或者,按最低日期值

      var date = db.Warehouse.Min(a => a.DeliveryTime);
      var lowest = db.Warehouse.FirstOrDefault(a =>  Ids.Contains(a.Id) && DateTime.Equals(a.DeliveryTime,date));
      

      【讨论】:

        猜你喜欢
        • 2010-09-25
        • 1970-01-01
        • 2011-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        • 2015-08-18
        • 2012-05-17
        相关资源
        最近更新 更多