开发者

max and group by question with LINQ

I want to group the below query by GetSetDomainName and select the row which has the maximum GetSetKalanGun.In other words, I am trying to get the row with the maximum KALANGUN among those which have the same DOMAINNAME.

var kayitlar3 = (
    from rows in islemDetayKayitListesi
    select new
    {
        KAYITNO = rows.GetSetKayitNo,
        HESAPADI = rows.GetSetHesapAdi,
        URUNNO = rows.GetSetUrunNo,
        URUNADI = rows.GetSetUrunAdi,
        URUNMIKTAR = rows.GetSetUrunMiktar,

        ISLEMTARIHI = rows.GetSetIslemTarihi,
        HIZMETDURUMU = rows.GetSetHizmetDurumu,
        TOPLAMTUTAR = rows.GetSetToplamTutar,

        HIZMETBASLANGICTARIHI = rows.GetSetHizmetBaslangicTarihi,
        HIZMETBITISTARIHI = rows.GetSetHizmetBitisTarihi,
        KALANGUN = rows.GetSetKalanGun 
        DOMAINNAME = rows.GetSetDomainName,
        SIPARISDURUMU = rows.GetSetSiparisDurumu
    }).AsQueryable();

This is what I get

KAYITNO DOMAINNAME KALANGUN
1       asdf.com      30
2       domnam.com    172
3       asdf.com      40
4       xyz.com       350

This is what I want

KAYITNO DOMAINNAME KALANGUN
 2       domnam.com    172
 3       asdf.com      40
 4       xyz.com       350

var islemDetayKayitListesi = ne开发者_StackOverflow社区w List<IslemDetayKayit>();

islemDetayKayitListesi get filled with a foreach loop, with no problem

And that is what IslemDetayKayit looks like

public class IslemDetayKayit
{
    public int GetSetKayitNo { get; set; }
    public string GetSetHesapAdi { get; set; }
    public string GetSetUrunNo { get; set; }
    public string GetSetUrunAdi { get; set; }
    public double GetSetUrunMiktar { get; set; }
    public string GetSetIslemTarihi { get; set; }
    public string GetSetHizmetDurumu { get; set; }
    public string GetSetToplamTutar { get; set; }
    public string GetSetHizmetBaslangicTarihi { get; set; }
    public string GetSetHizmetBitisTarihi { get; set; }
    public int GetSetKalanGun { get; set; }
    public string GetSetSiparisDurumu { get; set; }
    public string GetSetDomainName { get; set; }
}

EDIT : I figured out that there was some other problem in my code, and corrected it.After that all the answer I had to this question works.Thank you for helping and teaching me new things.


This will do the trick:

var q =
    from item in kayitlar3
    group item by item.DOMAINNAME into g
    select g.OrderByDescending(i => i.KALANGUN).First();

You can also try this:

var q = 
    from row in islemDetayKayitListesi
    group row by row.GetSetDomainName into g
    let highest = g.OrderByDescending(r => r.GetSetKalanGun).First()
    select new
    {
        KAYITNO = highest.GetSetKayitNo,
        DOMAINNAME = g.Key,
        KALANGUN = highest.GetSetKalanGun
    };

Note that this would yield the same results. If it doesn't, there is a problem with your code that we can't see by looking at the information that you posted.


You could use:

var kayitlar3 = 
    islemDetayKayitListesi.
    Select(rows => 
    new
    {
        KAYITNO = rows.GetSetKayitNo,
        HESAPADI = rows.GetSetHesapAdi,
        URUNNO = rows.GetSetUrunNo,
        URUNADI = rows.GetSetUrunAdi,
        URUNMIKTAR = rows.GetSetUrunMiktar,

        ISLEMTARIHI = rows.GetSetIslemTarihi,
        HIZMETDURUMU = rows.GetSetHizmetDurumu,
        TOPLAMTUTAR = rows.GetSetToplamTutar,

        HIZMETBASLANGICTARIHI = rows.GetSetHizmetBaslangicTarihi,
        HIZMETBITISTARIHI = rows.GetSetHizmetBitisTarihi,
        KALANGUN = rows.GetSetKalanGun,
        DOMAINNAME = rows.GetSetDomainName,
        SIPARISDURUMU = rows.GetSetSiparisDurumu
    }).
    GroupBy(a => 
        //To ignore case and trailing/leading whitespace
        a.DOMAINNAME.ToUpper().Trim()).
    Select(g => 
         g.OrderByDescending(a => a.KALANGUN).FirstOrDefault()).
    AsQueryable();

EDIT:

So using this code:

        List<Thing> islemDetayKayitListesi = new List<Thing>();
        Thing a = new Thing() { GetSetDomainName = "abc.com", GetSetKayitNo = 1,
            GetSetKalanGun = 40 };
        Thing b = new Thing() { GetSetDomainName = "abc.com", GetSetKayitNo = 2, 
            GetSetKalanGun = 300 };
        Thing c = new Thing() { GetSetDomainName = "xyz.com", GetSetKayitNo = 3, 
            GetSetKalanGun = 400 };
        Thing d = new Thing() { GetSetDomainName = "123.com", GetSetKayitNo = 4, 
            GetSetKalanGun = 124 };
        islemDetayKayitListesi.Add(a);
        islemDetayKayitListesi.Add(b);
        islemDetayKayitListesi.Add(c);
        islemDetayKayitListesi.Add(d);
        var kayitlar3 =
            islemDetayKayitListesi.
                Select(rows =>
                new
                {
                    KAYITNO = rows.GetSetKayitNo,
                    HESAPADI = rows.GetSetHesapAdi,
                    URUNNO = rows.GetSetUrunNo,
                    URUNADI = rows.GetSetUrunAdi,
                    URUNMIKTAR = rows.GetSetUrunMiktar,

                    ISLEMTARIHI = rows.GetSetIslemTarihi,
                    HIZMETDURUMU = rows.GetSetHizmetDurumu,
                    TOPLAMTUTAR = rows.GetSetToplamTutar,

                    HIZMETBASLANGICTARIHI = rows.GetSetHizmetBaslangicTarihi,
                    HIZMETBITISTARIHI = rows.GetSetHizmetBitisTarihi,
                    KALANGUN = rows.GetSetKalanGun,
                    DOMAINNAME = rows.GetSetDomainName,
                    SIPARISDURUMU = rows.GetSetSiparisDurumu
                }).
                GroupBy(anon =>
                    anon.DOMAINNAME).
                Select(g =>
                    g.OrderByDescending(anon => anon.KALANGUN).First()).
                AsQueryable();
        kayitlar3.ToList().
            ForEach(anon => Console.WriteLine("{0}, {1}, {2}", 
                anon.KAYITNO, anon.DOMAINNAME, anon.KALANGUN));

    struct Thing 
    {
        public int GetSetKayitNo { get; set; }
        public int GetSetHesapAdi { get; set; }
        public int GetSetUrunNo { get; set; }
        public int GetSetUrunAdi { get; set; }
        public int GetSetUrunMiktar { get; set; }

        public int GetSetIslemTarihi { get; set; }
        public int GetSetHizmetDurumu { get; set; }
        public int GetSetToplamTutar { get; set; }

        public int GetSetHizmetBaslangicTarihi { get; set; }
        public int GetSetHizmetBitisTarihi { get; set; }
        public int GetSetKalanGun { get; set; }
        public string GetSetDomainName { get; set; }
        public int GetSetSiparisDurumu { get; set; }
    }

I get the expected output:

2, abc.com, 300
3, xyz.com, 400
4, 123.com, 124


After clarification about your desired output, this will return the row with the top KALANGUN per DOMAINNAME:

var kayitlar3 = (
from rows in islemDetayKayitListesi
select new
{
    KAYITNO = rows.GetSetKayitNo,
    HESAPADI = rows.GetSetHesapAdi,
    URUNNO = rows.GetSetUrunNo,
    URUNADI = rows.GetSetUrunAdi,
    URUNMIKTAR = rows.GetSetUrunMiktar,

    ISLEMTARIHI = rows.GetSetIslemTarihi,
    HIZMETDURUMU = rows.GetSetHizmetDurumu,
    TOPLAMTUTAR = rows.GetSetToplamTutar,

    HIZMETBASLANGICTARIHI = rows.GetSetHizmetBaslangicTarihi,
    HIZMETBITISTARIHI = rows.GetSetHizmetBitisTarihi,
    KALANGUN = rows.GetSetKalanGun 
    DOMAINNAME = rows.GetSetDomainName,
    SIPARISDURUMU = rows.GetSetSiparisDurumu
})
.GroupBy(rr => rr.DOMAINNAME)
.SelectMany(gg => gg.OrderByDescending(rr => rr.KALANGUN).First());


Try

  from rows in islemDetayKayitListesi 
  group rows by new { rows.GetSetDomainName} into results
  let MaxKALANGUN = results.Max(i=>i.KALANGUN)
  select new 
  { 
        KAYITNO = results.First(i=>i.KALANGUN== MaxKALANGUN).GetSetKayitNo
        DOMAINNAME = results.Key.GetSetDomainName , 
        KALANGUN = MaxKALANGUN
  } 

If you want the complete class, try

  from rows in islemDetayKayitListesi 
  group rows by new { rows.GetSetDomainName} into results
  let MaxKALANGUN = results.Max(i=>i.KALANGUN)
  select results.First(i=>i.KALANGUN== MaxKALANGUN)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜