?
Solved

c# linq group labmda expression null issue

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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

764 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