How to return an int in a Decimal property

Asked

Viewed 301 times

1

I have an SQL string (made by another programmer a long time ago, so I can’t change it) that returns something like this: If it has value in column, returns value, otherwise returns 0 (I would post here the SQL but it is gigantic).

However, column with value is decimal, without value is integer. To pass it to C#, I created a model with the property of type Dynamic:

public dynamic valorRetornado { get; set; }

But it comes null. I tried with Object and also did not function.

Does anyone have any idea?

@Harrypotter is here:

//Data é a string aonde está o SQL
var dados = db.Database.SqlQuery<Produtos>(data);
                    foreach (var itens in dados)
                    {                        
                        listaProdutos.Add(customer);
                    }
  • 1

    Junior you could post the C# code that redeems such SQL ?

  • I’ll already put an example of how I would !!! Wait minutes

2 answers

3


So you should put it like this:

public Decimal valorRetornado { get; set; }

for the fact that I realized that you’re wearing a Entityframework to generate the data.

Something else if you’re going to return 0 (Zero) but, this column may contain values NULL do so:

public Decimal? valorRetornado { get; set; }

and let returns NULL, where it has no value.

Let’s go with a simple example

I have in my database a table with these characteristics:

inserir a descrição da imagem aqui

But I want to take 3 fields Id, Nome and Valor and create a class on my system for this using Entity Framework to run an SQL and bring this data:

Class Modelo

public class Exemplo
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public decimal? Valor { get; set; }
}

In this class the Value field can contain values Null, equal to the default setting in my database, which automatically Entityframework will treat that.

Coding:

IList<Exemplo> exemplos = db
              .Database
              .SqlQuery<Exemplo>("SELECT Id, Nome, Valor FROM Exemplos")
              .ToList<Exemplo>();

Please note that the table field name and its type must match your class so you don’t experience any conversion problems and errors

Data contained

inserir a descrição da imagem aqui

Data brought, via Debug Visual Studio to the created model class

inserir a descrição da imagem aqui

Note that you have 3 items, and the Id number 2 Value is NULL equal to your database.

  • 1

    I had never gotten very well this story (how/for what? use) do ? next to the type, ball show! + 1 for you! Valeuu!

  • @Harrypotter I did as Oce said but gives this error: The specified cast from a materialized 'System.Int32' type to the 'System.Decimal' type is not Valid

  • 1

    @Juniordias your problem may also be in SQL if you play 0 cast then to decimal, but in the class that receives the data it can not a dynamic field but a primitive type, it is quieter the coding work. My example above proves this... from a glance!

  • 1

    @Harrypotter Thank you very much for the examples!

1

You can’t treat everything to decimal?? it is very dangerous to use the type dynamic, if you end up forgetting some treatment will have exceptions...

I don’t know how you’re doing to read from the bank if it’s with DataRow or with the EntityFramework, but here comes a help...

(!dr.IsNull("coluna") ? Convert.ToDecimal(dr["coluna"]) : 0.0m)

  • if I do with Entity, how can I convert?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.