[dba-VB] ADO.NET Entity Model

Gustav Brock Gustav at cactus.dk
Sat Mar 7 08:41:25 CST 2009


Hi all

Also, the fill method is now vastly reduced.

    protected void OwnerDropDownList_Init(object sender, EventArgs e)
    {
        LocationDropDownList.DataValueField = "Id";
        LocationDropDownList.DataTextField = "Location1";
    }

    private void FillLocationDropDownList()
    {
        LocationDropDownList.DataSource = _erhvervslejeContext.LocationByOwnerQuery;
        LocationDropDownList.DataBind();
    }

/gustav


>>> Gustav at cactus.dk 07-03-2009 15:27 >>>
Hi all

I finally found out how to return Location rather than anonymous types:

  public ObjectQuery<Location> LocationByOwnerQuery
        {
            get
            {
                ObjectQuery <Location> locationByOwner;
                if (_locationFilterOwner != null)
                {
                    locationByOwner =
                        Location 
                        .Where("it.Owner.Id = @ownerId",
                        new ObjectParameter ("ownerId", _locationFilterOwner.Id));
                }
                else
                {
                    locationByOwner =
                        Location;
                }
                return locationByOwner;
            }
        }

Now, how could I use the object Owner as parameter and not Owner.Id?
This is not accepted:

 .Where("it.Owner = @owner", new ObjectParameter ("owner", _locationFilterOwner));

as only scalar values are accepted as parameter values.

/gustav


>>> Gustav at cactus.dk 06-03-2009 17:53 >>>
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