Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在差异处添加>;使用mongodb的.net api中的0_.net_Mongodb_Asp.net Core_Aggregation Framework_Asp.net Core Webapi - Fatal编程技术网

在差异处添加>;使用mongodb的.net api中的0

在差异处添加>;使用mongodb的.net api中的0,.net,mongodb,asp.net-core,aggregation-framework,asp.net-core-webapi,.net,Mongodb,Asp.net Core,Aggregation Framework,Asp.net Core Webapi,我有一流的产品 public class Product { [BsonId] public Object Id { get; set; } [BsonElement("sku")] public string Sku { get; set; } [BsonElement("name")] public string Name { get; set;

我有一流的产品

public class Product
    {
        [BsonId]
        public Object Id { get; set; }

        [BsonElement("sku")]
        public string Sku { get; set; }

        [BsonElement("name")]
        public string Name { get; set; }

        [BsonElement("price")]
        public double Price { get; set; }

        [BsonElement("store")]
        public int Store { get; set; }
    }
和班主任

 public class butter
    {

        public string Sku { get; set; }

        public string Name { get; set; }

        public double Maxprice { get; set; }

        public double MinPrice { get; set; }

        public double Diffrence { get; set; } // max -min
    }
我正在制作一个API,从产品中获取数据并以黄油的形式返回。 我正在尝试将差异>0添加到我的Api中

public IEnumerable<butter> Get()
         {
            var result = _productCollection.AsQueryable()
                              .OrderByDescending(e => e.Price)
                              .GroupBy(e => e.Sku)
                              .Select(g => new butter
                              {
                                  Sku= g.First().Sku,
                                  Name=g.First().Name,
                                 Maxprice=g.Max(x=>x.Price),
                                  MinPrice=g.Min(x=>x.Price),
                                  Diffrence= (g.Max(x => x.Price)- g.Min(x => x.Price))
                              }
                               )
                              .ToList();
            return result;
        }
public IEnumerable Get()
{
var result=\u productCollection.AsQueryable()
.OrderByDescending(e=>e.Price)
.GroupBy(e=>e.Sku)
.选择(g=>新黄油
{
Sku=g.First().Sku,
Name=g.First().Name,
Maxprice=g.Max(x=>x.Price),
MinPrice=g.Min(x=>x.Price),
差异=(g.Max(x=>x.Price)-g.Min(x=>x.Price))
}
)
.ToList();
返回结果;
}
你能帮我在api代码中添加Difference>0吗


谢谢你

我举个例子供你参考。因为我不知道
\u productCollection
的来源。根据这个模型
产品
,我在数据库中模拟了一些数据

因为它是按列Sku分组的,所以它被用作下一个查询的键。和
AsQueryable
应更改为
ToList
。下面是要查询的已更改的方法

public IEnumerable<butter> Get()
    {
        var result = _db.Products.ToList().OrderByDescending(e => e.Price).GroupBy(e => e.Sku).Select(x => new butter
                    {
                        Sku = x.Key,
                        Name = x.FirstOrDefault().Name,
                        Maxprice = x.Max(y => y.Price),
                        MinPrice = x.Min(y => y.Price),
                        Diffrence = x.Max(y => y.Price) - x.Min(y => y.Price)
                    }).Where(a => a.Diffrence > 0).ToList();
        return result;
    }
public IEnumerable Get()
{
var result=_db.Products.ToList().OrderByDescending(e=>e.Price).GroupBy(e=>e.Sku).Select(x=>new butter
{
Sku=x.键,
Name=x.FirstOrDefault().Name,
Maxprice=x.Max(y=>y.Price),
最小价格=x.Min(y=>y.Price),
差异=x.Max(y=>y.Price)-x.Min(y=>y.Price)
})其中(a=>a.差异>0).ToList();
返回结果;
}
由于
sku1
的最高价格等于最低价格,因此差异不会大于0。这是查询的结果


我尝试了以下方法,但没有成功