简体   繁体   English

为什么FirstOrDefault得到此异常? 序列不包含匹配元素异常

[英]Why FirstOrDefault get this exception? Sequence contains no matching element exception

I hope you can help me to my code after I change the data type of the following I get this exception "Sequence contains no matching element exception. ". 我希望您在更改以下数据类型后能对我的代码有所帮助,我得到此异常“序列不包含匹配的元素异常。”。 And I am sure this because of FirstOrDefault() Extension. 我确信这是因为FirstOrDefault()扩展。

LandId - long
ShowMapPoint - string
Development - string
Location - string
MapPointX - string
MapPointY - string
AreaSize - from decimal? into long?
Premium - from decimal? into long?
TransactionPrice - from decimal? into long?

This is my code: 这是我的代码:

var result = _context.DwPropertyMasters.Where(x => x.ShowMapPoint == "Y")
                .Select(x => new
                {
                    x.LandId,
                    a = x.Development == null || x.Development == "" ? x.Location : x.Development,
                    x.MapPointX,
                    x.MapPointY,
                    AreaSize = x.AreaSize ?? 0,
                    Premium = x.Premium ?? 0,
                    b = (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize == 0 ? null : x.AreaSize) ?? 0,
                    c =
                    _context.DwPropertyDetails.Where(
                            z => (z.TransactionPrice > 0 || z.TransactionPrice != null) && z.LandId == x.LandId)
                        .GroupBy(z => z.LandId)
                        .Select(g =>
                            (g.Sum(p => p.TransactionPrice) == 0 ? null : g.Sum(p => p.TransactionPrice)) /
                            (g.Sum(p => p.ActualSize) == 0 ? null : g.Sum(p => p.ActualSize)) ?? 0)
                        .FirstOrDefault(),
                    d =
                    ((x.AreaSize2 == 0 ? null : x.AreaSize2) == 0
                        ? 0
                        : (x.Premium == 0 ? null : x.Premium) * 100000000 / (x.AreaSize2 == 0 ? null : x.AreaSize2)) ??
                    0,
                    x.LandType,
                    e =
                    _context.DwPropertyDetails.Where(
                            y => (y.TransactionPrice > 0 || y.TransactionPrice != null) && y.LandId == x.LandId)
                        .Select(y => new
                        {
                            a = 1
                        }).Count()
                });

Your problem is in the sum of the Premium and TransactionPrice 您的问题在于PremiumTransactionPrice的总和

If you grouping doesn't contain an item with a value, then it will try to sum null values. 如果分组不包含带有值的项目,则它将尝试对空值求和。 That can't work. 那行不通。

I would change it as follows: 我将其更改如下:

(g.All(p => p.TransactionPrice == null) ? null : g.Where(p => p.TransactionPrice != null).Sum(p => p.TransactionPrice))

and

(g.All(p => p.ActualSize == null) ? null : g.Where(p => p.ActualSize != null).Sum(p => p.ActualSize)))

First check if everything is null, then it is null, else sum the values. 首先检查所有内容是否为null,然后检查是否为null,否则求和。

But you need to edit your code further, your current code would enable situations like 123 / null or null / 123 . 但是您需要进一步编辑代码,您当前的代码将启用123 / nullnull / 123 Which would also be a exception. 这也是一个例外。

It is hard to understand what you are doing. 很难理解你在做什么。 It is way too complex with all these inline if's. 所有这些内联if太复杂了。 And on line 13 you have: 在第13行上,您有:

z.TransactionPrice > 0 || z.TransactionPrice != null. 

You might as well remove z.TransactionPrice > 0, because the second part allows all values (not null), including everything smaller than 0. 您也可以删除z.TransactionPrice> 0,因为第二部分允许所有值(非null),包括所有小于0的值。

The 'Sequence contains no matching element' exception is typically something I'd expect when you use .First(). 当您使用.First()时,通常会期望'Sequence不包含任何匹配元素'异常。 Not .FirstOrDefault(). 不是.FirstOrDefault()。

Based on your code and what I think you want to achieve I have rewritten the query: 根据您的代码和我想达到的目标,我重写了查询:

var result = _context.DwPropertyMasters.Where(x => x.ShowMapPoint == "Y")
                .Select(x => new
                {
                    x.LandId,
                    a = x.Development == null || x.Development == "" ? x.Location : x.Development,
                    x.MapPointX,
                    x.MapPointY,
                    AreaSize = x.AreaSize ?? 0,
                    Premium = x.Premium ?? 0,
                    b = (x.AreaSize == 0) ? 0 : (x.Premium * 100000000 / x.AreaSize ?? 0),
                    c = _context.DwPropertyDetails.Where(z => z.TransactionPrice > 0 && z.LandId == x.LandId)
                        .GroupBy(z => z.LandId)
                        .Select(g => g.Sum(p => p.ActualSize) == 0 ? 0 : (g.Sum(p => p.TransactionPrice) / g.Sum(p => p.ActualSize) ?? 0)
                        .FirstOrDefault(),
                    d = (x.AreaSize2 == 0) ? 0 : (x.Premium * 100000000 / x.AreaSize2 ?? 0),
                    x.LandType,
                    e = _context.DwPropertyDetails.Count(y => y.TransactionPrice > 0 && y.LandId == x.LandId)
                });

I do not know if this works as you intended (and works at all since I didn't test this), but it is shorter and I hope more readable. 我不知道它是否按您的预期工作(并且因为我没有测试过,所以还是可以工作),但是它更短,希望可读性更好。

Keep in mind that this query is executed in sql server, so there is no need to check for null values. 请记住,此查询是在sql server中执行的,因此不需要检查null值。 The only thing that must be prevented is to divide by zero. 唯一必须防止的事情是除以零。 If we consider the next line: 如果我们考虑下一行:

b = (x.AreaSize == 0) ? 0 : (x.Premium * 100000000 / x.AreaSize ?? 0)

If: 如果:

  • x.AreaSize is null then (x.Premium * 100000000 / null ?? 0) => null ?? x.AreaSize为null则为(x.Premium * 100000000 / null ?? 0)=> null? 0 => 0. 0 => 0。

  • x.AreaSize = 0 then the result = 0. x.AreaSize = 0,然后结果= 0。

  • x.Premium is null then the result is null / value ?? x.Premium为null,则结果为null /值?? 0 => null ?? 0 =>空?? 0 => 0. 0 => 0。

  • x.Premium = 0 then the result = 0. x.Premium = 0,然后结果= 0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 序列不包含匹配元素,FirstOrDefault - Sequence contains no matching element, FirstOrDefault Xamarin:“序列不包含匹配元素”异常 - Xamarin: 'Sequence contains no matching element' Exception FirstOrDefault抛出'Sequence包含多个匹配元素' - FirstOrDefault throws 'Sequence contains more than one matching element' SingleOrDefault()引发异常序列包含多个匹配元素 - SingleOrDefault() Throw exception Sequence contains more than one matching element NHibernate 3.1 NHibernate.Linq.NhRelinqQueryParser异常“序列包含多个匹配元素” - NHibernate 3.1 NHibernate.Linq.NhRelinqQueryParser exception “Sequence contains more than one matching element” Include()ThenInclude()在Table Per Hierarchy策略中抛出“Sequence包含多个匹配元素”异常 - Include() ThenInclude() throws “Sequence contains more than one matching element” exception in Table Per Hierarchy strategy 序列不包含匹配元素 - Sequence contains no matching element Sequence包含多个元素异常,但在数据库中只有一个 - Sequence contains more than one element exception but in the database is just one saveChanges-序列不包含匹配元素 - saveChanges - Sequence contains no matching element ToList() 上的“序列不包含匹配元素” - “Sequence contains no matching element” on ToList()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM