Solved

c# linq group labmda expression null issue

Posted on 2015-02-16
2
88 Views
Last Modified: 2015-03-03
I have a collection that I am trying to group on.  If this statement does not return a value, I am trying to set the column to 0, but the group returns no results.
 whatIfScenarios.Find(x => x.CbiId == g.First().CbiId && x.Type == WhatIfScenarioType.Plan).WhatIfOutput.RiskMarkScore

here is my code

var group = (from r in riskMarkOutputModel.Recommendations
    where r.InPlan == true
    group r by r.CbiId
    into g
    select
        new
        {
            IndexRecNum = g.First().IndexRecNum,
            a = whatIfScenarios.Find(x => x.CbiId == g.First().CbiId && x.Type == WhatIfScenarioType.Plan).WhatIfOutput.RiskMarkScore ?? 0/*,

Open in new window



If I leave out the row "a", I get a group.  If I leave "a" in, the group is empty.  Even if the find is blank, I want the group to show.
0
Comment
Question by:jackjohnson44
2 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
Comment Utility
If Find returns null, you'd get an exception when trying to retrieve the WhatIfOutput property. Unless you're using C# 6, you need to check that before trying to access the property.
var group = (from r in riskMarkOutputModel.Recommendations
             where r.InPlan == true
             group r by r.CbiId
             into g
             let scenario = whatIfScenarios.Find(x => x.CbiId == g.First().CbiId && x.Type == WhatIfScenarioType.Plan)
             select
                 new
                 {
                     IndexRecNum = g.First().IndexRecNum,
                     a = scenario == null ? 0 : scenario.WhatIfOutput.RiskMarkScore
                 }

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now