我在linq中写这个查询,我想做这个工作 选择新的1和x和y 2-times timespans和数字中的多重播放,例如30
var query =
(from c in DB.SabtHoghoghs
join aa in DB.SabtHesabKarmands on c.KarmandID equals aa.KarmandID
select new
{
c.Karmand.FName,
c.BabatMah,
x = DB.SabtHesabKarmands.Where(q => q.BabatMah.Year == c.BabatMah.Year &&
q.BabatMah.Month == c.BabatMah.Month && q.HesabKarmand.HesabKarmandTypeID==1)
.Sum(aaa => aaa.Tedad * aaa.HesabKarmand.Mablagh),
y = DB.SabtHesabKarmands.Where(q => q.BabatMah.Year == c.BabatMah.Year &&
q.BabatMah.Month == c.BabatMah.Month && q.HesabKarmand.HesabKarmandTypeID == 2)
.Sum(aaa => aaa.Tedad * aaa.HesabKarmand.Mablagh),
z=y-x //how to do this(1)
karkard = (c.Karmand.ExitTime-c.Karmand.EnterTime).Hours *30 //how to do this(2)
}).Distinct();
答案 0 :(得分:0)
我会尽力指导您找到正确的解决方案。 定义你的自定义类,不要使用匿名类,如此
class MyClass
{
public string FName { get; set; }
public string BabatMah { get; set; }
public int x { get; set; }
public int y { get; set; }
public int z { get { return x - y; } }
public int karkard { get; set; }
// implement Equals and GetHashCode for correct behaviour of Distinct
}
然后只选择没有z
的计算值到MyClass
select new MyClass
{
FName = c.Karmand.FName,
BabatMah = c.BabatMah,
...
要解决(c.Karmand.ExitTime-c.Karmand.EnterTime).Hours *30
计算,您需要提供有关ORM的更多详细信息。解决方案将取决于。一般的解决方案是将ExitTime
和EnterTime
提取到MyClass
,然后以与计算x - y
答案 1 :(得分:0)
在查询中使用let
关键字:
var query =
(from c in DB.SabtHoghoghs
join aa in DB.SabtHesabKarmands on c.KarmandID equals aa.KarmandID
let x = DB.SabtHesabKarmands.Where(q => q.BabatMah.Year == c.BabatMah.Year &&
q.BabatMah.Month == c.BabatMah.Month && q.HesabKarmand.HesabKarmandTypeID == 1)
.Sum(aaa => aaa.Tedad * aaa.HesabKarmand.Mablagh)
let y = DB.SabtHesabKarmands.Where(q => q.BabatMah.Year == c.BabatMah.Year &&
q.BabatMah.Month == c.BabatMah.Month && q.HesabKarmand.HesabKarmandTypeID == 2)
.Sum(aaa => aaa.Tedad * aaa.HesabKarmand.Mablagh)
select new
{
c.Karmand.FName,
c.BabatMah,
X = x,
Y = y,
Z = y - x, //how to do this(1)
karkard = (c.Karmand.ExitTime - c.Karmand.EnterTime).Hours * 30 //how to do this(2)
}).Distinct();