Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
521 views
in Technique[技术] by (71.8m points)

c# - Select with null in previous properties

Is it possible to do:

IEnumerable<MyModel> models = context.MyDBModels
                .Include(x => x.ConnectionA)
                .Select(x => new MyModel{        
                    SomeData = new SomeDataClass 
                    {
                        prop1 = x.ConnectionA.ConnectionB.prop1,
                        prop2 = x.ConnectionA.ConnectionB.prop2,
                    }
                });

when ConnectionB for example is null since null propagating operator is not possible in tree expressions like these.

I tested it and it looks like it works (it doesn't throw exceptions and in prop1/prop2 I have null as I want), but I want to be sure


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When using LINQ to databases, the LINQ is translated to SQL (or other database query language) and the handling of null depends on that language. In the case of SQL, null propagation is essentially automatic, though you have to be aware if you switch to client side processing somewhere in the LINQ query.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...