JJNL77
JJNL77

Reputation: 159

SQL Query to Linq query with cast as real

Hello I now use the following SQL Query:

SELECT DateTime, CAST(FillLevel as REAL) as FillLevel, " +
                  "CAST(Temperature as REAL) as Temperature, " +
                  "CAST(Latitude as REAL) as Latitude, " +
                  "CAST(Longitude as REAL) as Longitude, " +
                  "CAST(Unit as REAL) as Unit from Message

Now I want to get the same result with a Linq query. What's the best way to CAST a type to REAL?

I now have the following:

var tempquery = from m in context.Message
                        where m.Unit == 1
                        select new
                        {
                            DateTime = m.DateTime,
                            FillLevel = m.FillLevel,
                            Temperature = m.Temperature,
                            Latitude = m.Latitude,
                            Longitude = m.Longitude,
                            Unit = m.Unit
                        };

Upvotes: 1

Views: 283

Answers (1)

Gert Arnold
Gert Arnold

Reputation: 109195

The C# equivalent of SQL's real is float (which is the type alias of Single). You can add the type casts to the query:

from m in context.Message
where m.Unit == 1
select new
{
    DateTime = m.DateTime,
    FillLevel = (float)m.FillLevel,
    Temperature = (float)m.Temperature,
    Latitude = (float)m.Latitude,
    Longitude = (float)m.Longitude,
    Unit = (float)m.Unit
};

Depending on the ORM you're using you will see this translated in something like:

SELECT CAST([m].[FillLevel] AS real)
...

Upvotes: 2

Related Questions