在Select new Class上设置成员属性

时间:2016-07-26 12:33:01

标签: c# entity-framework linq

我已经定义了一个自定义类(BRDResult),我想用它来从EF select中返回数据,这样只能从查询中返回几列,而不是从所有包含的表中返回所有列。 该类具有string,int等

形式的简单属性
results.Select(
            batchClaim => new BRDResult
            {
                ClaimSystemNumber = batchClaim.ClaimSystemNumber,
                TotalAllowedAmount = batchClaim.Claim.ClaimHeaderResult
                .Where(header => header.ClaimSystemNumber == claimSystemNumber && header.RunNumber == batchRunNumber).Sum(header => header.ClaimDetailResult.Select(detail => detail.AllowedAmount).Sum()),
                CisDetailLineNumber = batchClaim.Claim.ClaimHeaderResult
                .Where(header => header.ClaimSystemNumber == claimSystemNumber && header.RunNumber == batchRunNumber)
                .SelectMany(head => head.ClaimDetailResult)
                .Select(det => det.CisDetailNumber)
                .FirstOrDefault(),
                ReimbursementMethod = batchClaim.Claim.ClaimHeaderResult.Where(header => header.ClaimSystemNumber == claimSystemNumber && header.RunNumber == batchRunNumber).SelectMany(head => head.ClaimDetailResult).Select(det => det.ReimbursementMethod).FirstOrDefault(),
                PaymentStatusCode = batchClaim.Claim.ClaimMHeaderResult.Where(header => header.ClaimSystemNumber == claimSystemNumber && header.RunNumber == batchRunNumber).SelectMany(head => head.ClaimMDetailResult).Select(det => det.FinalEapgNumber.ToString()).FirstOrDefault(),
                PaymentPercent = batchClaim.Claim.ClaimMHeaderResult.Where(header => header.ClaimSystemNumber == claimSystemNumber && header.RunNumber == batchRunNumber).SelectMany(head => head.ClaimMDetailResult).Select(det => det.PaymentPercent).FirstOrDefault(),
                AllowedAmount = batchClaim.Claim.ClaimHeaderResult.Where(header => header.ClaimSystemNumber == claimSystemNumber && header.RunNumber == batchRunNumber).SelectMany(head => head.ClaimDetailResult).Select(det => det.AllowedAmount).FirstOrDefault(),
                PendCode = batchClaim.Claim.ClaimHeaderResult.Where(header => header.ClaimSystemNumber == claimSystemNumber && header.RunNumber == batchRunNumber).SelectMany(head => head.ClaimDetailResult).Select(det => det.PendCode).FirstOrDefault(),
                OptimizerReturnCode = batchClaim.Claim.ClaimMHeaderResult.Where(header => header.ClaimSystemNumber == claimSystemNumber
               && header.RunNumber == batchRunNumber).Select(head => head.OptimizerReturnCode.ToString()).FirstOrDefault()

            });

我知道FirstOrDefault限制只撤回BRDResult类型的一行,实际上我需要撤回多个详细记录。

但是,因为像CisDetailLineNumber这样的左侧成员属性是简单的字符串属性左右,所以使用Select返回IEnumerable本身引用ICollection.certain属性,我被迫使用FirstOrDefault来让构建工作。

从这里获取多个详细记录会很好,所提到的列在每个记录中都有相应的值。

现在2天后真的坚持这个。非常感谢任何帮助。

Input -> ClaimSystemNumber = 1369 and RunNumber = 1 (these are used in   repository.Query itself and returned entire data using just .Select())
Then the results.Select pulls a filtered column data from that

Current Output -> Single record in IEnumerable<BRDResult> with values
{ClaimSystemNumber = 1369,
CisDetailLineNumber = 1,
ReimbursementMethod = "APC",
PaymentStatusCode = "1",
PaymentPercent = 1.0,
AllowedAmount = 11,
PendCode = "23",
OptimizerReturnCode = "WE"}

Expected Result -> 2 records with different values in all the properties as shown above as detail table has 2 records matching given ClaimSystemNumber and RunNumber combination.

0 个答案:

没有答案