Solved

c# linq group labmda expression null issue

Posted on 2015-02-16
2
119 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
louisfr earned 500 total points
ID: 40630064
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

696 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