New Posts New Posts RSS Feed: How can I return an IEntityQuery type?
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

How can I return an IEntityQuery type?

 Post Reply Post Reply
Author
paul View Drop Down
Newbie
Newbie


Joined: 16-Sep-2010
Posts: 19
Post Options Post Options   Quote paul Quote  Post ReplyReply Direct Link To This Post Topic: How can I return an IEntityQuery type?
    Posted: 06-Apr-2012 at 11:57am
Thanks Silvio, if I read the manual I would of come across this sooner.

http://drc.ideablade.com/xwiki/bin/view/Documentation/query-include-predicate

My next question is I am following the practices of Cocktail's repository pattern. How best can I return an IEntityQuery type? I need to access my model's Person, Address and my RegionLocalized.FullDescription property in my View. My entity model associations look like this:

Employee --> Person --> Address --> Region --> RegionLocalizeds

Technically I could bypass the Region entity since my Address entity has a RegionId property that could join to my RegionLocalized entity that has the FullDescription property I need to pull back for the address state name.

I tried this earlier once I read the documentation but I need to return some kind of IEntityQuery.

            IEntityQuery<?????> query = Manager
                .Employees
                .Where(e => e.EmployeeId == Guid.Parse("2661C686-66CD-4E7A-8E5C-1C0EA43F1D6B"))
                .Select(e => new
                {
                    Person = e.Person,
                    Address = e.Person.Address,
                    RegionLocalized = e.Person.Address.Region.RegionLocalizeds.Where(rl => rl.RegionId == e.Person.Address.RegionId)
                });

            EntityQueryOperation<Model.Employee> queryOperation;
            yield return queryOperation = query.ExecuteAsync();

            yield return Coroutine.Return(queryOperation.Results.First());

Thanks...

Back to Top
paul View Drop Down
Newbie
Newbie


Joined: 16-Sep-2010
Posts: 19
Post Options Post Options   Quote paul Quote  Post ReplyReply Direct Link To This Post Posted: 10-Apr-2012 at 12:21pm
Thanks Silvio for moving this post over. Is there any Cocktail pros that could chime in on a good practice to follow? Thanks...
Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 10-Apr-2012 at 12:30pm
Paul,
You are doing an anonymous projection query. Anonymous queries are nice, problem is the returned objects are anonymous, so you can't refer to them by type. To program against the returned data, you must define your own custom type and project into it or cast it to object and use reflection to access its properties. 

So, the first option is to cast it to object. If you are just gonna bind it to the view than that is sufficient, because XAML binding uses reflection and it will see the properties on your anonymous objects.

            IEntityQuery query = Manager
.Employees
.Where(e => e.EmployeeId == Guid.Parse("2661C686-66CD-4E7A-8E5C-1C0EA43F1D6B"))
.Select(e => new
{
Person = e.Person,
Address = e.Person.Address,
RegionLocalized = e.Person.Address.Region.RegionLocalizeds.Where(rl => rl.RegionId == e.Person.Address.RegionId)
});

    EntityQueryOperation queryOperation;
    yield return queryOperation = query.ExecuteAsync();

    yield return Coroutine.Return(queryOperation.Results.Cast<object>().First());

The second option is to project into a custom type. 

            IEntityQuery<MyCustomType> query = Manager
.Employees
.Where(e => e.EmployeeId == Guid.Parse("2661C686-66CD-4E7A-8E5C-1C0EA43F1D6B"))
.Select(e => new MyCustomType
{
Person = e.Person,
Address = e.Person.Address,
RegionLocalized = e.Person.Address.Region.RegionLocalizeds.Where(rl => rl.RegionId == e.Person.Address.RegionId)
});

    EntityQueryOperation< MyCustomType> queryOperation;
    yield return queryOperation = query.ExecuteAsync();

    yield return Coroutine.Return(queryOperation.Results.First());

The type should look like this:

[DataContract]
public class  MyCustomType : IKnownType
{
[DataMember]
public ??? Person { get;set; }

[DataMember]
public ??? Address { get;set; }

[DataMember]
public ??? RegionLocalized { get;set; }
}


Back to Top
mgood View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 18-Nov-2010
Location: Emeryville, CA
Posts: 583
Post Options Post Options   Quote mgood Quote  Post ReplyReply Direct Link To This Post Posted: 10-Apr-2012 at 12:38pm
BTW, the StaffingResourceSearchRepository in TempHire uses a projection query as an example. It projects into StaffingResourceListItem.
    [Export(typeof(IStaffingResourceSearchRepository)), PartCreationPolicy(CreationPolicy.NonShared)]
    public class StaffingResourceSearchRepository : IStaffingResourceSearchRepository
    {
        private readonly IEntityManagerProvider<TempHireEntities> _entityManagerProvider;
 
        [ImportingConstructor]
        public StaffingResourceSearchRepository(IEntityManagerProvider<TempHireEntities> entityManagerProvider)
        {
            _entityManagerProvider = entityManagerProvider;
        }
 
        private TempHireEntities EntityManager
        {
            get { return _entityManagerProvider.Manager; }
        }
 
        #region IStaffingResourceSearchRepository Members
 
        public OperationResult<IEnumerable<StaffingResourceListItem>> FindStaffingResourcesAsync(
            string searchText, string orderBy, Action<IEnumerable<StaffingResourceListItem>> onSuccess = null,
            Action<Exception> onFail = null)
        {
            IEntityQuery<StaffingResource> baseQuery = EntityManager.StaffingResources;
 
            if (!string.IsNullOrWhiteSpace(searchText))
                baseQuery = baseQuery.Where(resource => resource.FirstName.Contains(searchText) ||
                                                        resource.MiddleName.Contains(searchText) ||
                                                        resource.LastName.Contains(searchText) ||
                                                        resource.Addresses.FirstOrDefault(a => a.Primary).Address1.
                                                            Contains(searchText) ||
                                                        resource.Addresses.FirstOrDefault(a => a.Primary).Address2.
                                                            Contains(searchText) ||
                                                        resource.Addresses.FirstOrDefault(a => a.Primary).City.Contains(
                                                            searchText) ||
                                                        resource.Addresses.FirstOrDefault(a => a.Primary).Zipcode.
                                                            Contains(searchText) ||
                                                        resource.Addresses.FirstOrDefault(a => a.Primary).State.Name.
                                                            Contains(searchText) ||
                                                        resource.Addresses.FirstOrDefault(a => a.Primary).State.
                                                            ShortName.Contains(searchText) ||
                                                        resource.PhoneNumbers.FirstOrDefault(p => p.Primary).AreaCode.
                                                            Contains(searchText) ||
                                                        resource.PhoneNumbers.FirstOrDefault(p => p.Primary).Number.
                                                            Contains(searchText));
 
            var query =
                baseQuery.Select(resource => new StaffingResourceListItem
                                                 {
                                                     Id = resource.Id,
                                                     FirstName = resource.FirstName,
                                                     MiddleName = resource.MiddleName,
                                                     LastName = resource.LastName,
                                                     Address1 =
                                                         resource.Addresses.FirstOrDefault(a => a.Primary).Address1,
                                                     Address2 =
                                                         resource.Addresses.FirstOrDefault(a => a.Primary).Address2,
                                                     City = resource.Addresses.FirstOrDefault(a => a.Primary).City,
                                                     State =
                                                         resource.Addresses.FirstOrDefault(a => a.Primary).State.
                                                         ShortName,
                                                     Zipcode = resource.Addresses.FirstOrDefault(a => a.Primary).Zipcode,
                                                     AreaCode =
                                                         resource.PhoneNumbers.FirstOrDefault(p => p.Primary).AreaCode,
                                                     Number =
                                                         resource.PhoneNumbers.FirstOrDefault(p => p.Primary).Number
                                                 });
 
            if (!string.IsNullOrWhiteSpace(orderBy))
                query = query.OrderBySelector(new SortSelector(orderBy));
 
            return query.ExecuteAsync(op => op.OnComplete(onSuccess, onFail)).AsOperationResult();
        }
 
        #endregion
    }
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down