我使用linQ从数据库获得不同的价格 [英] I wana get distinct prices from database using linQ
本文介绍了我使用linQ从数据库获得不同的价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public List<Product> GetProductPrices()
{
return (from x in context.Products select x).OrderBy(x=>x.Price).ToList<Product>();
}
it is my function kindly help me
推荐答案
嗨阿卜杜勒,>
只需像这样修改你的查询。
Hi Abdul,
Just modify your query like this.
return (from x in context.Products select x).Distinct().OrderBy(x=>x.Price).ToList<product>();
</product>
希望这会有所帮助。
Hope this helps.
请尝试这个解决方案,
创建一个类似于数据库中字段的类。
喜欢这个类
Please try this solution,
Create a class that similar to your fields in database.
Like this one
//Products class
public class Products
{
public Int32 ID { get; set; }
public String Name { get; set; }
public Double Price { get; set; }
}
//Products comparer class.
class Comparer<t> : IEqualityComparer<t>
{
private Func<t,> _funcDistinct;
public Comparer(Func<t,> funcDistinct)
{
this._funcDistinct = funcDistinct;
}
public bool Equals(T x, T y)
{
return _funcDistinct(x).Equals(_funcDistinct(y));
}
public int GetHashCode(T obj)
{
return this._funcDistinct(obj).GetHashCode();
}
}
</t></t>
并且使用这个类你的linq查询就像这样(注意我假设你已经从数据库中获取数据了。
and the use this class ni your linq query like this (note i am assuming you are already getting data from database).
var prod = (from p in lstProducts select p).Distinct(new Comparer<products>(p => p.Price))</products>.OrderBy(p => p.Price).ToList<products>();
</products>
我认为这可以解决你的问题。
I think this will solve your problem.
你可以使用这样的东西。
you can use somthing like this.
var result = (from item in List
select new
{
EmpLoc = item.empLoc,
EmpPL= item.empPL,
EmpShift= item.empShift
})
.ToList()
.Distinct();
这篇关于我使用linQ从数据库获得不同的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文