我希望在我的linq查询中使用distinct的完整行详细信息

时间:2015-06-18 09:18:41

标签: c# linq distinct

我有一张包含财产详情

的表格
------------------------------------------------
Id | Propertyname |date_created |Price |agent_name
------------------------------------------------
1  |xxxxxx        |17-06-2015   |10000 |abc
2  |xxyyyyy       |15-06-2015   |10000 |abc
3  |xxyyyyy       |16-06-2015   |10000 |bcd
4  |xxyyyyy       |15-06-2015   |10000 |bcd
5  |xxyyyyy       |17-06-2015   |10000 |cde
6  |xxyyyyy       |15-06-2015   |10000 |cde

我想要一个结果

------------------------------------------------
    Id | Propertyname |date_created |Price |agent_name
    ------------------------------------------------
    1  |xxxxxx        |17-06-2015   |10000 |abc
    3  |xxyyyyy       |16-06-2015   |10000 |bcd
    5  |xxyyyyy       |17-06-2015   |10000 |cde

我想要一个具有完整行详细信息的agent_name的结果

我试过下面的代码

var property = 
    from l in properties
    group l by l.agent_name into g
    let lastUsed = g.OrderBy(x => x.date_created).Last()
    select lastUsed;

Adove代码显示错误

LINQ to Entities无法识别方法'属性Lastproperties'方法,并且此方法无法转换为商店表达式。

任何人都知道如何解决这个问题

2 个答案:

答案 0 :(得分:1)

在LinqPad上创建了以下代码并且它可以工作,让我弄清楚是否有更好的,更优化的解决方案版本

void Main()
{   
    var result = Detail.Create();

    var final = result.GroupBy(s=>s.agent).ToDictionary(p=>p.Key,p=>p.OrderBy(n=>n.dateTime).Last())
                                          .Select(a=>new 
                                            { 
                                            a.Value.id,
                                            a.Value.prop,
                                            a.Value.dateTime,
                                            a.Value.price,
                                            agent = a.Key
                                            });


    foreach(var x in final)
    {
      Console.WriteLine(x.id + " :: " + x.prop + " :: " + x.dateTime+ " :: " + x.price+ " :: " +x.agent);
    }
}

public class Detail
{
 public Detail(int i, string p, DateTime d, int pr, string a)
 {
   id= i;
   prop = p;
   dateTime = d;
   price = pr;
   agent = a;
 }
  public int id;
  public string prop;
  public DateTime dateTime;
  public int price;
  public string agent;

  public static  List<Detail> Create()
  {
    List<Detail> list = new List<Detail>();

    list.Add(new Detail(1, "xxxxxx", DateTime.Parse("17-06-2015"), 10000,"abc"));
    list.Add(new Detail(2, "xxyyyyy", DateTime.Parse("15-06-2015"), 10000,"abc"));
    list.Add(new Detail(3, "xxyyyyy", DateTime.Parse("16-06-2015"), 10000,"bcd"));
    list.Add(new Detail(4, "xxyyyyy", DateTime.Parse("15-06-2015"), 10000,"bcd"));
    list.Add(new Detail(5, "xxyyyyy", DateTime.Parse("17-06-2015"), 10000,"cde"));
    list.Add(new Detail(6, "xxyyyyy", DateTime.Parse("15-06-2015"), 10000,"cde"));

    return list;
   }
}

答案 1 :(得分:1)

var property=(from n in bc.db.properties select n).GroupBy(x => x.agent_name)
      .Where(g => g.Count() == 1)
      .Select(g => g.FirstOrDefault()).OrderByDescending(x=>x.date_created)