0

I have the below code:

public static List<ResponseSupplier> Feza_ReturnSuppliers(string country, string locale, string rtype, string scat, string sscat, string bill, int? comp, double? inc, int? loi, bool? fc, int? hf)
    {
        string query = string.Format(Queries[SqlQueries.returnquery], country, locale, rtype, scat, sscat, bill, comp, inc, loi, fc, hf);
        IList<ExpandoObject> suppliersRaw = DatabaseHelper.MultiResponseComplex(conn, query,
            new List<string>
            {   "SupID",
                "SupName",
                "Tlabel",
                "Tcode",
                "dcstatic",
                "dcur",
                "dse",
                "ltz",
                "st",
                "isamp",
                "minf",
                "sf",
                "cpt",
                "Scoring",
                "TrustCoefficient",
                "ApiIntegrated",
                "fapi"
            });

        return suppliersRaw.Select(e => new ResponseSupplier

        {
            SupID = (((dynamic)e).SupID.ToString()) as string,
            SupName = (((dynamic)e).SupName) as string,
            Tlabel = (((dynamic)e).TierLabel) as string,
            Tcode = (int?)(((dynamic)e).Tcode),
            dcstatic = (double?)(((dynamic)e).dcstatic),
            dcur = (((dynamic)e).dcur) as string,
            dse = (((dynamic)e).dse) as string,
            ltz = (((dynamic)e).ltz) as string,
            st = (((dynamic)e).st) as string,
            isamp = (bool?)(((dynamic)e).isamp),
            minf = (double?)(((dynamic)e).minf),
            sf = (double?)(((dynamic)e).sf),
            cpt = (string)(((dynamic)e).cpt),
            Scoring = (double?)(((dynamic)e).Scoring),
            TrustCoefficient = (double?)(((dynamic)e).TrustCoefficient),
            ApiIntegrated = (bool?)(((dynamic)e).ApiIntegrated),
            fapi = (bool?)(((dynamic)e).fapi)

    }).ToList();

I get the following error: 'Cannot convert type 'System.DBNull' to 'double?'' That is because in the db i can have Null for: 'TrustCoefficient' and 'Scoring'. How can i get past the error, please note that i do not want to get a value like '0' when is null. That is because i need to compare the results of this list with a different one (and that one displays null for the mentioned parameters). Is there a way to convert or parse or do something with

            Scoring = (double?)(((dynamic)e).Scoring),
            TrustCoefficient = (double?)(((dynamic)e).TrustCoefficient),

To allow DBNull and in the result to show Scoring = Null ?

Banu
  • 21
  • 1
  • 1
  • 6
  • Possible duplicate of [Can I cast from DBNull to a Nullable Bool in one line?](https://stackoverflow.com/questions/14609921/can-i-cast-from-dbnull-to-a-nullable-bool-in-one-line) – mjwills Aug 02 '18 at 09:25
  • 1
    Also consider using an ORM like PetaPoco or Dapper that does this for you automatically. – mjwills Aug 02 '18 at 09:25
  • Looks like the real problem is `MultiResponseComplex` and whatever it does? Why not use an anonymous type or a microORM like Dapper so you *don't* need all those casts and conversions? – Panagiotis Kanavos Aug 02 '18 at 09:26

1 Answers1

0

Try this:

Scoring = ((dynamic)e).Scoring is DBNull ? null : (double)(((dynamic)e).Scoring),
TrustCoefficient = (dynamic)e).TrustCoefficient is DBNull ? null : (double)(((dynamic)e).TrustCoefficient),

And you will need to check if every column value is DBNull

Alan haha
  • 491
  • 1
  • 4
  • 10