Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IQueryable for Anonymous Types

I use EntityFramework, I'm querying and returning partial data using Anonymous Types. Currently I'm using IQueryable<dynamic>, it works, but I would like to know if this is the proper way to do it or if there is some other returning datatype that I'm not aware of.

public IQueryable<dynamic> FindUpcomingEventsCustom(int daysFuture)
{
    DateTime dateTimeNow = DateTime.UtcNow;
    DateTime dateTimeFuture = dateTimeNow.AddDays(daysFuture);
    return db.EventCustoms.Where(x => x.DataTimeStart > dateTimeNow & x.DataTimeStart <= dateTimeFuture)
        .Select(y => new { y.EventId, y.EventTitle, y.DataTimeStart});
}
like image 733
GibboK Avatar asked Aug 16 '12 07:08

GibboK


People also ask

How to declare anonymous type?

Typically, when you use an anonymous type to initialize a variable, you declare the variable as an implicitly typed local variable by using var. The type name cannot be specified in the variable declaration because only the compiler has access to the underlying name of the anonymous type.

How to specify anonymous type C#?

You create an anonymous type using the new operator with an object initializer syntax. The implicitly typed variable- var is used to hold the reference of anonymous types.

When to use an anonymous type in LINQ?

Anonymous types provide a convenient way to encapsulate a set of read-only properties in an object without having to explicitly define a type first. If you write a query that creates an object of an anonymous type in the select clause, the query returns an IEnumerable of the type.

What are anonymous data types?

Anonymous types are a feature of C# 3.0, Visual Basic . NET 9.0, Oxygene, Scala and Go that allows data types to encapsulate a set of properties into a single object without having to first explicitly define a type. This is an important feature for the SQL-like LINQ feature that is integrated into C# and VB.net.


1 Answers

Normally, you use anonymous types only inside the scope of one method. You don't return anonymous types to the caller. If that's what you want to do, you should create a class and return that:

public class Event
{
    private readonly int _eventId;
    private readonly string _eventTitle;
    private readonly DateTime _dateTimeStart;

    public Event(int eventId, string eventTitle, DateTime dateTimeStart)
    {
        _eventId = eventId;
        _eventTitle = eventTitle;
        _dateTimeStart = dateTimeStart;
    }

    public int EventId { get { return _eventId; } }
    public string EventTitle { get { return _eventTitle; } }
    public DateTime DateTimeStart{ get { return _dateTimeStart; } }
}



public IQueryable<Event> FindUpcomingEventsCustom(int daysFuture) 
{ 
    DateTime dateTimeNow = DateTime.UtcNow; 
    DateTime dateTimeFuture = dateTimeNow.AddDays(daysFuture); 
    return db.EventCustoms
             .Where(x => x.DataTimeStart > dateTimeNow
                         && x.DataTimeStart <= dateTimeFuture) 
             .Select(y => new Event(y.EventId, y.EventTitle, y.DataTimeStart)); 
} 
like image 115
Daniel Hilgarth Avatar answered Sep 29 '22 13:09

Daniel Hilgarth