Solved

c# linq group labmda expression null issue

Posted on 2015-02-16
2
91 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
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 video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

939 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

6 Experts available now in Live!

Get 1:1 Help Now