如果值为null,如何在.Where()扩展中设置零值

时间:2017-02-21 05:13:18

标签: c# linq

现在我弄清楚当x.transaction为null时它会跳过。但是如何在.Where()扩展方法中设置一个值?如您所见,如果id不为null,则第一行具有条件,然后将该值设置为零。但是如果x.TransactionPrice为null,如何在.Where()扩展中如何设置值呢?在第三行。

就在这个代码示例中:

var Rs12 = id != null
                ? _context.DwPropertyDetails
                    .Where(x => x.LandId == id && x.TransactionPrice != null)
                    .OrderByDescending(x => x.TransactionPrice)
                    .AsEnumerable()
                    .Select(
                        (x, index) =>
                            new
                            {
                                TRANSACTION_PRICE = x.TransactionPrice ?? (long?)0,
                                ACTUAL_SIZE = x.ActualSize,
                                rank = index + 1
                            })
                    .Where(x => x.rank == 1).Select(x => new
                    {
                        TRAN_S = x.TRANSACTION_PRICE ?? (long?)0
                    })
                    .SelectMany(
                        TranS =>
                            _context.DwPropertyDetails.Where(
                                    x => x.LandId == id && x.TransactionPrice != null)
                                .OrderByDescending(
                                    x =>
                                        (x.TransactionPrice ?? 0) /
                                        (x.ActualSize == null || x.ActualSize == 0 ? 1 : x.ActualSize) ?? 1)
                                .AsEnumerable()
                                .Select(
                                    (x, index) =>
                                        new
                                        {
                                            PER_FT_S =
                                            (x.TransactionPrice ?? 0) /
                                            (x.ActualSize == null || x.ActualSize == 0 ? 1 : x.ActualSize) ?? 1,
                                            rank = index + 1
                                        })
                                .Where(x => x.rank == 1).Select(x => new
                                {
                                    x.PER_FT_S
                                }), (TranS, PerFtS) => new
                                {
                                    TranS.TRAN_S,
                                    PerFtS.PER_FT_S
                                })
                : _context.DwPropertyDetails.Select(x => new
                {
                    TRAN_S = (long?)0,
                    PER_FT_S = (long)0
                });

1 个答案:

答案 0 :(得分:0)

.Where()扩展名接受谓词,因此您可以使用statement lambdas代替expression lambdas并设置值。

class A
    {

        public int Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string AddressPresent { get; set; }
    }

这里是在其中设置值的方法

static void WhereDemo()
        {
            List<A> aList = new List<A>{

                    new A{ Id=1,Name="Hi",Address="Toronto" },
                    new A{ Id=2,Name="Him",Address="NY" },
                    new A{ Id=3,Name="His" },
                    new A{ Id=4,Name="quad",Address="MS" },
            };

          var queryOutput=  aList.Where(x => {
                if (x.Address != null)
                {
                    x.AddressPresent = "Present";
                    return true; // selects the element
                }
                else
                {
                    x.AddressPresent = "Absent";
                    return false; // does not select the element
                }

            });

            foreach (var element in aList)
            {

                Console.WriteLine("{0}\t{1}\t{2}\t{3}", element.Id, element.Name, element.Address, element.AddressPresent);
            }
            Console.WriteLine("-----------------------");
            foreach (var item in queryOutput)
            {
                Console.WriteLine("{0}\t{1}\t{2}\t{3}", item.Id, item.Name, item.Address, item.AddressPresent);

            }
        }

输出

1       Hi      Toronto
2       Him     NY
3       His
4       quad    MS
-----------------------
1       Hi      Toronto Present
2       Him     NY      Present
4       quad    MS      Present

您可以观察到它只影响查询输出,但不影响原始列表元素。