New Posts New Posts RSS Feed: Query for Ids In (...)
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Query for Ids In (...)

 Post Reply Post Reply
Author
EisenbergEffect View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Aug-2010
Location: Tallahassee, FL
Posts: 11
Post Options Post Options   Quote EisenbergEffect Quote  Post ReplyReply Direct Link To This Post Topic: Query for Ids In (...)
    Posted: 20-Aug-2010 at 11:03am
Here's the sample code I have been trying with no success:

var idList = ids.ToArray();
var query = context.Entities.GetQuery<SomeEntity>().Where(y => idList.Contains(y.Id));

I get serialization errors whethere I try this as a List<T> or array. Is there a workable way to search for all entities whose Id property is in a list?

I should mention that context.Entities is my strongly typed EntityManager and that I am doing this from Silverlight 4.


Edited by EisenbergEffect - 02-Sep-2010 at 11:28am
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 20-Aug-2010 at 6:47pm
Found out why your example didn't work. The exception explains all ...
 
"System.InvalidOperationException: DevForce does not support 'Contains' for array types.  Try converting the array to a List<T> with the Linq ToList() method."
 
Perhaps you didn't look at that exception. Or perhaps I'm using a preview build of DF 6.0.5 which gives a much clearer explanation of the problem.
 
Here's a synchronous query test that shows it works with a list of ids ... instead of the array. Yes ... the async query version of the test passes as well.
    [TestMethod]
    public void then_can_get_employees_by_id_list() {
      var mgr = CreateNewConnectedManager();

      var idArray = new [] { 1, 3, 5}; // won't work with arrays
      var idList = idArray.ToList(); // but works fine with a list

      var emps = mgr.Employees
        .Where(emp => idList.Contains(emp.EmployeeID))
        .ToList();
      emps.Count.ShouldEqual(3);
    }
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 20-Aug-2010 at 6:50pm
Ok ... I know you want to see the async version. I ran in it async in the Desktop ... would be same in Silverlight except for the Silverlight Unit Test Framework goo would be different than my goo.
 
    [TestMethodAsynchronousTimeout(15000)]
    public void then_can_get_employees_asynchronously_by_id_list() {

      DoItAsync(delegate {

        var mgr = CreateNewConnectedManager();
        var idArray = new[] { 1, 3, 5 }; // won't work with arrays
        var idList = idArray.ToList(); //   but works fine with a list

        var op = mgr.Employees
          .Where(emp => idList.Contains(emp.EmployeeID))
          .ExecuteAsync();

        op.Completed += (s, args) => {
          Assert.IsFalse(args.HasError, "Query exception: " + args.Error);
          var emps = args.Results;
          emps.Count().ShouldEqual(3);

        Done();
        };


      });

    }
Back to Top
EisenbergEffect View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Aug-2010
Location: Tallahassee, FL
Posts: 11
Post Options Post Options   Quote EisenbergEffect Quote  Post ReplyReply Direct Link To This Post Posted: 23-Aug-2010 at 7:05am
Unfortunately, this does not work. I've tried it with Arrays, Lists, Observables and just using the ToQueryable method. The result is always the same. I get an error such as the following:

There was an error while trying to serialize parameter http://ideablade.com/EntityModel:entityQuerySurrogate. The InnerException message was 'Type 'System.Collections.Generic.List`1[[System.Int32, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]' with data contract name 'ArrayOfint:http://schemas.microsoft.com/2003/10/Serialization/Arrays' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

I'm using version 6.0.4


Edited by EisenbergEffect - 23-Aug-2010 at 7:12am
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 23-Aug-2010 at 10:25am
The technique described here definitely works ... on all of our machines. Something is not quite right in your environment. Following up with you outside the forum. I'll be back with the resolution for our genernal audience.
Back to Top
kimj View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 09-May-2007
Posts: 1391
Post Options Post Options   Quote kimj Quote  Post ReplyReply Direct Link To This Post Posted: 23-Aug-2010 at 11:13am
Support for Contains was added in current bits and will be available in release 6.0.5 coming in a few weeks.  To work around that now you can manually add your collection type as a known type.  You can do this by implementing an IKnownTypeProvider, or adding a KnownType attribute for the type of your collection to an existing type.  Eg,
 
[KnownType(typeof(List<int>))]
public partial class SomeEntity { }
Back to Top
EisenbergEffect View Drop Down
Newbie
Newbie
Avatar

Joined: 20-Aug-2010
Location: Tallahassee, FL
Posts: 11
Post Options Post Options   Quote EisenbergEffect Quote  Post ReplyReply Direct Link To This Post Posted: 23-Aug-2010 at 12:26pm
Excellent! It's working now.
Back to Top
WardBell View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 31-Mar-2009
Location: Emeryville, CA,
Posts: 338
Post Options Post Options   Quote WardBell Quote  Post ReplyReply Direct Link To This Post Posted: 23-Aug-2010 at 12:29pm
Thanks, KimJ. That will teach me to answer questions with bits no one has. Sorry folks.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down