简体   繁体   English

c# Linq 链与避免 null 异常

[英]c# Linq Chain with avoid null exeption

i have multi tables with one to many relation like chain我有多个表,具有一对多关系,例如链

1- address has postal code id 1- 地址有邮政编码 id

2- postal table has area id 2-邮政表有区域ID

3- area table has city id 3-区域表有城市ID

4- city table has county id 4-城市表有县ID

5- county table has country id 5- 县表有国家 ID

6- and at the last country table 6- 在最后一个国家表

from every table i need to get street name, full postal code, area name, city name, county name, country name从每张表中我需要获取街道名称、完整的邮政编码、地区名称、城市名称、县名、国家名称

the query is as follow查询如下

var address = from add in _Database.Addresses
                      select add;

        address.Select(x=>new AddressClass { 
        BuildingNameOrNumber=x.BuildingNameOrNumber,
        MainStreet = x.Postcode ==null ? string.Empty: x.Postcode.StreetName,
        FullPostCode = x.Postcode == null ?  string.Empty :x.Postcode.FullPostcode,
        AreaName = x.Postcode == null ? string.Empty : x.Postcode.Area == null ? string.Empty: x.Postcode.Area.Name,
        CityName = x.Postcode == null ? string.Empty : x.Postcode.Area == null ? string.Empty : x.Postcode.Area.City == null ? string.Empty:  x.Postcode.Area.City.Name,
        CountyName = x.Postcode == null ? string.Empty : x.Postcode.Area == null ? string.Empty : x.Postcode.Area.City == null ? string.Empty : x.Postcode.Area.City.County == null ?string.Empty: x.Postcode.Area.City.County.Name,
        CountryName= x.Postcode == null ? string.Empty : x.Postcode.Area == null ? string.Empty : x.Postcode.Area.City == null ? string.Empty : x.Postcode.Area.City.County == null ? string.Empty : x.Postcode.Area.City.County.Country == null ? string.Empty:x.Postcode.Area.City.County.Country.CountryName
        })

i need to replace this multi conditions with one condition for every property我需要用每个属性的一个条件替换这个多重条件

This is too complex for me to wrap my head around sorry:)这对我来说太复杂了,抱歉:)

But what you need is the ?但你需要的是? and ???? operators.运营商。 Second to last item in your really complex query will be like:您真正复杂的查询中的倒数第二项将如下所示:

CountyName = x.Postcode?.Area?.City?.County?.Name ?? string.Empty

x?.y returns y if x is not null and returns null otherwise.如果x不是nullx?.y返回y ,否则返回null If y is not nullable then result of x?.y will become Nullable<T> where T is typeof(y)如果y不可为nullable ,则x?.y的结果将变为Nullable<T>其中Ttypeof(y)

x?? y x?? y returns x if x is not null and returns y if it is.如果x不是null ,则x?? y返回x ,如果是则返回y Type of x and y should be the same here. xy的类型在这里应该相同。 Hope it helps希望能帮助到你

Edit编辑

In the second look I see that you are using LINQ with IQueriable which can't use the null propagating operator.在第二个外观中,我看到您正在使用 LINQ 和IQueriable ,它不能使用 null 传播运算符。 So in this case you don't have this option you can either load all the data using a ToList() run your query on them if the data size is small.因此,在这种情况下,您没有此选项,如果数据量很小,您可以使用ToList()加载所有数据,对它们运行查询。 Or you can load each part in separate queries (which due to the number of sub element I don't recommend).或者您可以在单独的查询中加载每个部分(由于我不推荐子元素的数量)。 Alternatively you can use tools like this或者,您可以使用这样的工具

My advice is the first option however may not be memory friendly on large datasets.我的建议是第一个选项,但在大型数据集上可能不适合 memory。

Sorry for the initial hasty answer.很抱歉最初的仓促回答。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM