Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

c# linq group labmda expression null issue

Posted on 2015-02-16
2
Medium Priority
?
158 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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