[dba-VB] ADO.NET Entity Model

Gustav Brock Gustav at cactus.dk
Fri Mar 6 10:53:28 CST 2009


Hi all, sorry being serious on a Friday, but this is really bugging me ...

I wish to retrieve a list of locations for the DataSource of a DropDownList.
The entity model does have a collection of Location which I can retrieve and cast to a list like this:

> In .. .aspx.cs file:

    private void FillLocationDropDownList()
    {
        var locationQuery = (from l in _erhvervslejeContext.Location
                             select new
                            {
                                Id = l.Id,
                                Location1 = l.Location1
                            }).ToList();
        LocationDropDownList.DataSource = locationQuery; 
        LocationDropDownList.DataBind();
    }

However, I have added an ObjectQuery to the entity model, which filters Location, LocationByOwnerQuery (shown below):

    private void FillLocationDropDownList()
    {
        var locationQuery = (from l in _erhvervslejeContext.Location
                             select new
                            {
                                Id = l.Id,
                                Location1 = l.Location1
                            }).ToList();
        LocationDropDownList.DataSource = locationQuery; 
        LocationDropDownList.DataBind();
    }


Now, if I try to retrieve this as a list as shown, an error is raised at the var line:

" .. Cannot create a constant value of the type 'erhvervslejeModel.erhvervslejeEntities'.
Only primitive types ('like Int32, String og Guid') are supported in this context. .."

I know I could just do a foreach loop to add the items into the listbox but that is just a bit too primitive.

Any suggestions?

/gustav

> in     public partial class erhvervslejeEntities : global::System.Data.Objects.ObjectContext

        public ObjectQuery<Location> LocationByOwnerQuery
        {
            get
            {
                IQueryable query;
                if (_locationFilterOwner != null)
                {
                    query =
                        from
                            Location location in this.Location
                        where
                            location.Owner.Equals(_locationFilterOwner)
                        select 
                            location;
                }
                else
                {
                    query =
                        from
                            Location location in this.Location
                        select
                            location;
                }
                return (query as ObjectQuery<Location>);
            }
        }

***





More information about the dba-VB mailing list