我想按GetSetDomainName
对下面的查询进行分组,并选择最大GetSetKalanGun
的行。换句话说,我试图获得具有最大KALANGUN的行相同的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();
这就是我得到的
KAYITNO DOMAINNAME KALANGUN
1 asdf.com 30
2 domnam.com 172
3 asdf.com 40
4 xyz.com 350
这就是我想要的
KAYITNO DOMAINNAME KALANGUN
2 domnam.com 172
3 asdf.com 40
4 xyz.com 350
var islemDetayKayitListesi = new List<IslemDetayKayit>();
islemDetayKayitListesi
充满了foreach循环,没有问题
这就是IslemDetayKayit
的样子
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; }
}
编辑:我发现我的代码中还有其他一些问题,并对其进行了纠正。之后,我对这个问题的所有答案都有效。感谢您帮助并教我新事物
答案 0 :(得分:5)
这样可以解决问题:
var q =
from item in kayitlar3
group item by item.DOMAINNAME into g
select g.OrderByDescending(i => i.KALANGUN).First();
你也可以试试这个:
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
};
请注意,这会产生相同的结果。如果没有,那么您的代码存在问题,我们无法通过查看您发布的信息来查看。
答案 1 :(得分:4)
您可以使用:
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();
编辑:
所以使用这段代码:
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; }
}
我得到了预期的输出:
2, abc.com, 300
3, xyz.com, 400
4, 123.com, 124
答案 2 :(得分:1)
在澄清了您想要的输出后,这将返回每个DOMAINNAME顶部KALANGUN的行:
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());
答案 3 :(得分:1)
尝试
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
}
如果您想要完整的课程,请尝试
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)