Solved

LINQ syntax - where condition?

Posted on 2013-01-15
19
446 Views
Last Modified: 2013-01-15
decimal dec = Convert.ToDecimal(_repairBids.Select(o => o.BidAmt == null ? 0 : o.BidAmt).Sum(o => o));


if I wanted to filter this by "o.Status = "Approved" so that only bid amounts that are approved get totalled...what would be the syntax?
0
Comment
Question by:knowlton
  • 11
  • 4
  • 4
19 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 38780118
decimal dec = Convert.ToDecimal(_repairBids.Where(o.Status == "Approved").Select(o => o.BidAmt == null ? 0 : o.BidAmt).Sum(o => o));
0
 
LVL 5

Author Comment

by:knowlton
ID: 38780162
That looks correct to me.

If you don't mind helping me out, I keep getting:

value cannot be null

I tried to make provisions for this by adding this:

o.BidAmt == null ? 0 : o.BidAmt

but I must either be doing that wrong or the problem is elsewhere...


decimal dec = Convert.ToDecimal(_repairBids.Where(o => o.BidStatus == "Approved").Select(o => o.BidAmt == null ? 0 : o.BidAmt).Sum(o => o));

Open in new window



cannot be null
0
 
LVL 5

Author Comment

by:knowlton
ID: 38780175
Does this make sense?


decimal dec = Convert.ToDecimal(_repairBids.Where(o => o.BidStatus == "Approved").Where(o => o.BidAmt != null).Select(o => o.BidAmt).Sum(o => o));


can you do multiple "WHERE"'s like that?
0
 
LVL 22

Expert Comment

by:p_davis
ID: 38780180
do you know what is actually null?

you can try to check every level of that  object for null

o=> o == null  && ....?

also is it possible that the bidstatus is null? you might want to check for null in the where clause as well.
0
 
LVL 22

Expert Comment

by:p_davis
ID: 38780187
you can combine those two where clauses into one with &&
0
 
LVL 5

Author Comment

by:knowlton
ID: 38780200
null means "has never had a value" right?

[EDIT]

In other words, null is not 0, "0" or ""
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38780216
I thought BidAmt was a string? You can't mix types when using the conditional operator. Change the zero to a string:

decimal dec = Convert.ToDecimal(_repairBids.Where(o.Status == "Approved").Select(o => o.BidAmt == null ? "0" : o.BidAmt).Sum(o => o));

Open in new window

0
 
LVL 5

Author Comment

by:knowlton
ID: 38780217
>>  also is it possible that the bidstatus is null?

I supposed it could happen.  Better to be safe.  I'll check on that...
0
 
LVL 5

Author Comment

by:knowlton
ID: 38780228
>>>>I thought BidAmt was a string? You can't mix types when using the conditional operator. >>>>Change the zero to a string:


in one result it WAS indeed a string.


in another it is an actual decimal.


Let me recheck that....
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Author Comment

by:knowlton
ID: 38780251
This line compiles fine, but at runtime I still get the error:  value cannot be null

decimal dec = Convert.ToDecimal(_repairBids.Where(o => o.BidStatus == "Approved" && o.BidStatus != null && o.BidAmt != null).Select(o => o.BidAmt).Sum(o => o));

Open in new window



What am I missing?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38780252
For readability's sake, I'd suggest breaking that logic up a bit (once the issue is corrected):

decimal dec;

var sum = _repairBids.Where(o.Status == "Approved")
                     .Select(o => o.BidAmt ?? "0")
                     .Sum(o => o);

dec = Convert.ToDecimal(sum);

Open in new window

0
 
LVL 5

Author Comment

by:knowlton
ID: 38780257
Does it matter where the WHERE is called in the chain?

For example, does it matter if the WHERE is before the SELECT in LINQ?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38780262
It can matter, but I don't think it does in this case.

What is the type of BidAmt?
0
 
LVL 5

Author Comment

by:knowlton
ID: 38780278
>>>What is the type of BidAmt?


It is:

decimal?

which I interpret to mean it can be a decimal value or a null value returned
0
 
LVL 5

Author Comment

by:knowlton
ID: 38780299
NOTE:

The exception message, if it helps at all:

System.ArgumentNullException was unhandled by user code
  Message=Value cannot be null.
Parameter name: source
  Source=System.Core
  ParamName=source
  StackTrace:
       at System.Linq.Enumerable.Where[TSource](IEnumerable`1 source, Func`2 predicate)
       at Reo.ContentControls.RepairBidFormSummary.ReturnTot() in C:\R13\Reo\ContentControls\RepairBidFormSummary.ascx.cs:line 81
       at Reo.ContentControls.Preservation.FillRepairBid() in C:\R13\Reo\ContentControls\Preservation.ascx.cs:line 266
       at Reo.ContentControls.Preservation.FillPg() in C:\R13\Reo\ContentControls\Preservation.ascx.cs:line 113
       at Reo.ContentControls.Preservation.Page_Load(Object sender, EventArgs e) in C:\R13\Reo\ContentControls\Preservation.ascx.cs:line 68
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 250 total points
ID: 38780446
which I interpret to mean it can be a decimal value or a null value returned
Correct.

Are you certain that _repairBids is not null when you get to that line?
0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 250 total points
ID: 38780451
Of it is nullable (?) Then check the hasvalue property on it in the vlauses
0
 
LVL 5

Author Comment

by:knowlton
ID: 38780481
>>>Are you certain that _repairBids is not null when you get to that line?

Good news!!

Yes...this was the case.  _repairBids was null.  The entire thing.

I guess I didn't realize I needed to re-get everything from the data context.


Here is my final code:


 public decimal ReturnTot()
        {
            decimal dec = 0;

            using (PreservationDataContext dc = new PreservationDataContext())
            {
                _repairBids = dc.Pres_RepairBidFormSummary_Get(this.RepairBidFormID, this.RepairBidFormSID).ToList<Pres_RepairBidFormSummary_GetResult>();

                if (_repairBids != null)
                {
                    dec = Convert.ToDecimal(_repairBids.Where(o => o.BidStatus == "Approved" && o.BidStatus != null && o.BidAmt != null).Select(o => o.BidAmt).Sum(o => o));
                }
            }
            
            return dec;            
        }

Open in new window

0
 
LVL 5

Author Closing Comment

by:knowlton
ID: 38780488
Nice work, all of you!
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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

911 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

21 Experts available now in Live!

Get 1:1 Help Now