在差异处添加>;使用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。这是查询的结果
我尝试了以下方法,但没有成功