how to do the Select new {}

Posts   
 
    
JohannM
User
Posts: 6
Joined: 04-Jun-2009
# Posted on: 15-Oct-2009 15:19:05   

Hi all

I am trying to do the following code:-

    public List<ClientRegionEntity> GetClients()
    {
        var metadata = GetDataContext();

        var Contracts = (from cr in metadata.ClientRegion
                         select new {IDClient = cr.IDClient});

        return (List<ClientRegionEntity>) Contracts;
    }

Its a simple LINQ select, and I do not need the select new {}, however I am doing it because I will need to use it in another more complex query.

When I run this code, I am getting the following error:-

Unable to cast object of type 'SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery1[<>f__AnonymousType21[System.Int32]]' to type 'System.Collections.Generic.List`1[Enhesa.BusinessObjects.Adapter.EntityClasses.ClientRegionEntity]'.

I would like someone to point me in the right direction

Thanks for your help and tim

Johann

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 16-Oct-2009 06:07:49   

You are no executing the query. Add a .ToList() at the end of the query. Read the docs for more infp.

David Elizondo | LLBLGen Support Team
JohannM
User
Posts: 6
Joined: 04-Jun-2009
# Posted on: 16-Oct-2009 08:08:52   

daelmo wrote:

You are no executing the query. Add a .ToList() at the end of the query. Read the docs for more infp.

I did add a .ToList() but still got the same error.

What did the trick for me was

return Contracts as (List<ClientRegionEntity>) ;