Manu
Manu

Reputation: 1470

Missing a cast in a linq query for a controller

I am new with c# controllers and I am trying to join two entities with a LINQ query. But I am getting a 'missing a cast' error message as shown below that I don't understand and it does not help me to correct the error. enter image description here

The controller looks like:

    public class SoonDueReportsController_simple : BaseODataController
{
    public SoonDueReportsController_simple(IUnitOfWork unitOfWork)
    {
        UnitOfWork = unitOfWork;
    }

    [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    public SoonDueReportsController_simple Get()
    {

        var falligkeiten = UnitOfWork.GetAll<Fälligkeiten>();
        var betriebe = UnitOfWork.GetAll<Betriebe>();

        var query = (from betrieb in betriebe.AsQueryable()
                    join fallig in falligkeiten.AsQueryable()
                    on betrieb.ID equals
                    fallig.Betrieb_ID
                    where fallig.Fälligkeit_Erledigt_Datum == null
                    && betrieb.Aktiv == true
                    select new
                    {
                        BetriebId = betrieb.ID,
                        FalligkeitObject = fallig.Fälligkeit_Objekt
                    });
        return query;
    }
}

This type of controller I have used with success for single entities (tables from an sql db) to display static data in a kendo grid. But I fail when I try to join two tables as shown above. If someone could help me with this problem I'd appreciate it very much. Regards, Manu

Upvotes: 0

Views: 172

Answers (2)

Michael Puckett II
Michael Puckett II

Reputation: 6749

The exception explains to you the problem exactly. You're wanting to return a type of 'SoonDueReportsController_simple' and yet you are returning a Queryable where a' is your new { ..., ... } object.

I like the suggestion given to make a strong typed object and fill it but you can also return a dynamic type.

This code works to explain:

private dynamic Get() => new { Name = "SomeName", Age = 31 };

private void TestGet()
{
   var obj = Get();
   var name = obj.Name;
   var age = obj.Age;
}

Upvotes: 1

Alexey Zimarev
Alexey Zimarev

Reputation: 19610

You select a collection of anonymous objects

select new
{
    BetriebId = betrieb.ID,
    FalligkeitObject = fallig.Fälligkeit_Objekt
});

And want the method to return a instance of certain type. C# is the strongly typed language without type inference, which means you have to specifically create objects of a certain type or interface if you want to return them.

Also, you are have the controller type itself to be returned from the Get method. This makes no sense. I actually do not know what you want to do but may be this would work:

public class SoonDueReportsController_simple : BaseODataController
{
    public SoonDueReportsController_simple(IUnitOfWork unitOfWork)
    {
        UnitOfWork = unitOfWork;
    }

    [Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    public IQueryable<SomeModel> Get()
    {
        var falligkeiten = UnitOfWork.GetAll<Fälligkeiten>();
        var betriebe = UnitOfWork.GetAll<Betriebe>();

        var query = (from betrieb in betriebe.AsQueryable()
                    join fallig in falligkeiten.AsQueryable()
                    on betrieb.ID equals
                    fallig.Betrieb_ID
                    where fallig.Fälligkeit_Erledigt_Datum == null
                    && betrieb.Aktiv == true
                    select new SomeModel
                    {
                        BetriebId = betrieb.ID,
                        FalligkeitObject = fallig.Fälligkeit_Objekt
                    });
        return query;
    }
}

public class SomeModel 
{
    public int BetriebId { get; set; }
    public string FalligkeitObject { get; set; }
}

Please bear in mind that there are no such things as "C# controllers". You are working with OData, so I would recommend you to look at some OData resources, there are plenty of examples out there.

And one last thing, don't get me wrong, but it does not help giving properties, types and other identifiers German names. People would have hard time trying to understand your code.

Upvotes: 2

Related Questions