0

我在 linq 中编写了这个查询,我想在选择新的 2 减时间跨度中执行这项工作 1-sum x 和 y,并在一个数字中进行多重播放,例如 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();
4

2 回答 2

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 的更多详细信息。解决方案将取决于。一种通用的解决方案是提取ExitTimeEnterTime进入MyClass,然后以与您计算的相同的方式计算小时差x - y

于 2013-03-21T12:56:38.390 回答
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();
于 2013-03-21T13:04:20.663 回答