CodeEngine
CodeEngine

Reputation: 296

How to get a column which type is float using linq to entities

I'm new to EF and Linq to entities. I want to get a column which name is "Value" which type is a float. My code is shown below. So when I called the below method I get an error on the line double price..... Input string was not in a correct format. Is there a way to return the column "Value" as a double? or how can I convert it to a Double. Thanks in advance.

 public double calculate_price(int code, int quatity)
    {
        using( var context = new DryTypeEntities())
        {
            var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value) ;            
            string something = result.ToString();
            double price = Convert.ToDouble(something);
            double quant = Convert.ToDouble(quatity);
            double total = price * quant; 
            return total ;                             
        }

    }

Upvotes: 0

Views: 2477

Answers (3)

Jonathan Wood
Jonathan Wood

Reputation: 67283

This error means that A) something is a string and not a double, and B) the contents of something are not in the correct format to be converted to a double.

You haven't shown what something contains but that's what you should be looking at.

So guessing at what you want, you might want to do something like this:

using( var context = new DryTypeEntities())
{
    var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value) ;            
    foreach (var r in result)
    {
        double price = Convert.ToDouble(r);
        // There is no quatity returned by your query
    }
}

Note 1: Your query specifically selects Value, and so the result only contains the Value column. There is no quatity value returned by the query.

Note 2: Your query returns a collection (a group of zero or more elements), not just one element. I think this is what you really want:

using( var context = new DryTypeEntities())
{
    var result = context.Table_Products.FirstOrDefault(p => p.Code == 3);            
    return result.Value * result.quantity;
}

Upvotes: 1

Grant Winney
Grant Winney

Reputation: 66489

This returns a collection of Value's:

 var result = context.Table_Products
                     .Where(p => p.Code == 3)
                     .Select(p => p.Value);

This, then, stores the string representation of your collection in something (not what you intended):

string something = result.ToString();
    // System.Collections.Generic.List`1[System.Int32] or something equally unhelpful

That cannot be converted to a price, so this fails:

double price = Convert.ToDouble(something);   // uh, no.

Instead, one option is to get the first record:

 var result = context.Table_Products
                     .Where(p => p.Code == 3)
                     .Select(p => p.Value);
                     .First();

Or, if there's guaranteed to be exactly one match for "code" 3:

 var result = context.Table_Products
                     .Where(p => p.Code == 3)
                     .Select(p => p.Value);
                     .Single();

Now result contains one Value and you can work with that single value.

There's other stuff to consider too (i.e. what if there's no matching records, what if there are multiple records to iterate through, etc.), but this should get you going again.

One last point - consider using decimal for money values instead of double.

Upvotes: 0

scartag
scartag

Reputation: 17680

The select will not return the value you expect.

Change to.

var result = context.Table_Products.Where(p => p.Code == 3).Select(p => p.Value).FirstOrDefault() ; 

You probably should check that result isn't null.

Upvotes: 1

Related Questions