【问题标题】:I want full row detail with use of distinct in my linq query我想要在我的 linq 查询中使用 distinct 的完整行详细信息
【发布时间】:2015-06-18 09:18:40
【问题描述】:

我有一张包含房产详细信息的表格

------------------------------------------------
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 无法识别方法“properties Lastproperties”方法,并且该方法无法转换为存储表达式。

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

【问题讨论】:

  • 您包含的代码中没有 properties LastProperties 方法,所以我猜您的问题出在其他地方。简短的回答是 EF 不能将任意方法转换为 SQL。
  • properties 是表名

标签: c# linq distinct


【解决方案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)

【讨论】:

    【解决方案2】:

    在 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;
       }
    }
    

    【讨论】:

    • 感谢 Mrinal Kamboj 的回答。但我发现了一个单行 linq 查询
    • 欢迎您!能否请您发布我正在尝试弄清楚如何优化解决方案
    • 我发布了我的答案,如果你想请检查一下
    • 好的,谢谢,请检查我的更新,您只需要 Linq 部分,我已经优化了查询,最好使用 Fluent 语法
    猜你喜欢
    • 2020-08-24
    • 1970-01-01
    • 2023-02-07
    • 2022-06-15
    • 2011-09-24
    • 2022-11-21
    • 2022-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多