1

I have null values in database specifically for Good Expiry Date, which is the field is:good_expiryDate.

What i want is to handle it in this way: If good_expiryDate is null i want to present it as "" or "-", if it has value it will show the value retrieved from database?

the LINQ query i write is like this, but it return MinValue if good_expiryDate is null(this is what i tried to do).

var query = (from cd in db.CDIndexes
             join gds in db.Goods on cd.cdin_CDIndexID equals gds.good_CDIndexId 
             join itms in db.Items on gds.good_ItemsID equals itms.item_ItemsID

            where
            cd.cdin_CompanyId==c.Comp_CompanyId&&
            (
                gds.good_RemainCBM > 0 ||
                gds.good_RemainWT > 0 ||
                gds.good_RemainPackages > 0 ||
                gds.good_RemainVolumeWT > 0
            )
            &&
            itms.item_ItemsID==customerId

            select new DataItem
            {
                 depNumber=(int)cd.cdin_Serial,
                 ItemDesc=gds.good_Name,
                 gdExpiryDate =(DateTime) gds.good_expiryDate==null?DateTime.MinValue: (DateTime)gds.good_expiryDate,
                 InvoicBalanceUnits = (decimal) gds.good_RemainPackages,
                 WTBal=(decimal)gds.good_RemainWT,
                 VolWT=(decimal)gds.good_RemainVolumeWT
            }
       );
return query.ToList();

Update: good_expiryDate is type of Datetime, nullable in database

10
  • does your good_expiryDate is a nullable datetime ?? Commented Dec 28, 2016 at 7:29
  • gdExpiryDate should be string type. In this case you should use (DateTime)gds.good_expiryDate == null ? "-" : ((DateTime)gds.good_expiryDate).ToString("G") Commented Dec 28, 2016 at 7:31
  • May be you can try good_expiryDate = good_expiryDate.HasValue ? good_expiryDate.Value : null; Commented Dec 28, 2016 at 7:31
  • @MohitShrivastava yes it's Commented Dec 28, 2016 at 7:31
  • Can you compare with System.DBNull.Value instead of null Commented Dec 28, 2016 at 7:31

2 Answers 2

1

Ensure that gdExpiryDate is of type string.

Then change this line

gdExpiryDate =(DateTime) gds.good_expiryDate==null?DateTime.MinValue: (DateTime)gds.good_expiryDate,

to

gdExpiryDate = gds.good_expiryDate==null? "-" : ((DateTime)gds.good_expiryDate).ToString(),
Sign up to request clarification or add additional context in comments.

6 Comments

You only mentioned about your requirement like this If good_expiryDate is null i want to present it as "" or "-", if it has value it will show the value retrieved from database. If you want to display - then it has to be string obviously. @faresAyyad
I am speaking about gdExpiryDate and not good_expiryDate. good_expiryDate can be date time. But the gdExpiryDate should be string in order to keep the "-" value in it. @faresAyyad
Thank you, this was helpful
Welcome. Glad to help.
but how can i present it as dd/mm/yyyy
|
0

Change gdExpiryDate type to string type and write like this:

    select new DataItem
             {
                 depNumber=(int)cd.cdin_Serial,
                 ItemDesc=gds.good_Name,
                 gdExpiryDate = gds.good_expiryDate == null ? "" :  gds.good_expiryDate.Value.ToString("dd.mm.yyyy"),
                 InvoicBalanceUnits = (decimal) gds.good_RemainPackages,
                 WTBal=(decimal)gds.good_RemainWT,
                 VolWT=(decimal)gds.good_RemainVolumeWT
             }

1 Comment

I can't change the database field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.