Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an anonymous type as T

In the Entity Framework you can run and bind custom queries on-the-fly like so...

protected class NitrogenMoisutreContainer
{
    public double MinN { get; set; }
    public double MaxN { get; set; }
    public double MinM { get; set; }
    public double MaxM { get; set; }
}

// ...

var q = dbcontext.Database.SqlQuery<NitrogenMoisutreContainer>(@"SELECT MAX(NitrogenBalance) as MaxN, MIN(NitrogenBalance) as MinN, MAX(FCWaterPercent) as MaxM, MIN(FCWaterPercent) as MinM
                                                                    FROM agZoneProjectionGrowthStages
                                                                    WHERE NitrogenBalance > 0 AND FCWaterPercent > 0").First();

The problem is that, to me, this feels messy. I had to create this class for one query and I will never use it again for anything else. The results are used exactly one line down from where it is executed.

Is there a way I can return an anonymous type? Even if I had to declare it first, like this...

var anonItem = new {
    MinN = 0d,
    MaxN = 0d,
    MinM = 0d,
    MaxM = 0d
};

var q = dbcontext.Database.SqlQuery<anonItem.GetType()>("...");

I just can't figure out how to pass in my anonymous type's Type as T. Is it possible?

like image 778
jocull Avatar asked Sep 05 '12 20:09

jocull


People also ask

How do you define anonymous type?

Essentially an anonymous type is a reference type and can be defined using the var keyword. You can have one or more properties in an anonymous type but all of them are read-only. In contrast to a C# class, an anonymous type cannot have a field or a method — it can only have properties.

What is anonymous type of functions?

In C#, an anonymous type is a type (class) without any name that can contain public read-only properties only. It cannot contain other members, such as fields, methods, events, etc. You create an anonymous type using the new operator with an object initializer syntax.

What is the difference between an anonymous type and a regular data type?

From the perspective of the common language runtime, an anonymous type is no different from any other reference type, except that it cannot be cast to any type except for object.


2 Answers

This can be done if you set it up so the type can be inferred. E.g.

public static IEnumerable<T> GetObjects<T>(T exampleItem, string sqlQuery) { ... }
var q = GetObjects(anonItem, @"");

I think this is a bit hackish, and I'd suggest trying another approach, like actually making the named class or using a tuple.

like image 118
Tim S. Avatar answered Oct 01 '22 02:10

Tim S.


The problem is that, to me, this feels messy. I had to create this class for one query and I will never use it again for anything else. The results are used exactly one line down from where it is executed.

I don't see this as a problem.

Is there a way I can return an anonymous type?

You could create a tuple instead.

like image 37
Jim G. Avatar answered Oct 01 '22 04:10

Jim G.