locked
Create a custom class in a linq query in silverlight RRS feed

  • Question

  • How can I run a LINQ query in a silverlight application  that will generate a list of a local class?

    For example in server side code I have something like;

            public static List<TeamDescription> ListLeaveTeams2(IOrganizationService service, Guid userid)
            {
                List<TeamDescription> qtu = new List<TeamDescription>();
    
                // context for query
                MyContext ctx = new MyContext(service);
    
                // a query to list all teams
                var queryTeams = from tm in ctx.TeamMembershipSet
                                 join u in ctx.SystemUserSet on tm.SystemUserId.Value equals u.SystemUserId.Value
                                 join t in ctx.TeamSet on tm.TeamId.Value equals t.Id
                                 where (tm.SystemUserId == userid)
                                 where t.AdministratorId.Id != userid
                                 select new TeamDescription(tm.TeamId, t.Name, u.FullName, tm.SystemUserId.Value, t.AdministratorId.Id);
    
                // Some loop through that data - must be a better way to do this!
                foreach (TeamDescription team in queryTeams)
                {
                    qtu.Add(team);
                }
    
                return qtu;
            }
    

    And that works, qtu is a list of the local class TeamDescription. I am trying to do something similar in a silverlight application. e.g.;

    binding = new DataServiceCollection<CaseView>();
    binding.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(Cases_LoadCompleted);
    
    var query = from c in config.context.IncidentSet
                select new CaseView() { CustomerName = c.CustomerId.Name, Description = c.Description, CaseNumber = c.TicketNumber, ServiceLevel = c.PriorityCode.ToString(), LoggedTime = c.CreatedOn.Value, ResolveBy = c.qubic_servicemoule_fixdeadline.Value, Status = c.qubic_case_Status.ToString(), Queue = c.qubic_servicemodule_Queue.Name, Owner = c.OwningUser.Name };
    binding.LoadAsync(query);
    

    and I get all kind of errors, Unable to bind to c.CustomerID.Name, property names must be identical etc.

    How can I create a list of my local custom class in silverlight and linq

    Ta

    Wednesday, February 12, 2014 1:16 PM

All replies

  • Hi,

    The Silverlight LINQ implementation is very different to the early bound implementation you are using on the Server. The server side code is using the WCF SOAP calls and is much more sophisticated that the client implementation. The Silverlight Client LINQ query is compiling to an ODATA query that doesn't have the same flexibility when using projection as you are using.

    You will need to simplify your projection down to only use the attributes on the entity you are querying. See the example at http://msdn.microsoft.com/en-us/library/gg309558.aspx

    Hope this helps,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Thursday, February 13, 2014 10:35 AM
    Answerer
  • Hi,

    The Silverlight LINQ implementation is very different to the early bound implementation you are using on the Server. The server side code is using the WCF SOAP calls and is much more sophisticated that the client implementation. The Silverlight Client LINQ query is compiling to an ODATA query that doesn't have the same flexibility when using projection as you are using.

    You will need to simplify your projection down to only use the attributes on the entity you are querying. See the example at http://msdn.microsoft.com/en-us/library/gg309558.aspx

    Hope this helps,

    Scott


    Scott Durow
    Blog www.develop1.net    Follow Me
    Rockstar365
    If this post answers your question, please click "Mark As Answer" on the post and "Mark as Helpful"

    Does that mean that LINQ queries using join to get some composite object are not going to be supported in silverlight, i.e. basically I can't do something like ...

    select new MyObject() { Name = table.name, otherpropery = somejoinedtable.property, ... etc.)

    We are effectively limited to only retreiving copies of the entities as they are in CRM and then making any composite object at the client side?

    Thursday, February 13, 2014 12:06 PM