none
turn results into a custom object type RRS feed

  • Question

  • I need to return some results from an linq to sql query to a third party over a WCF interface and sending the autogenerated classes is not an option.

    So I tried the following:

     

    var members = from tm in context.TeamMembers
            where tm.TeamId == myTeam.TeamId
            select new ClientInterface.TeamMember
            {
             MemberId = tm.MemberId,
             UserId = tm.UserId,
             Number = tm.Number,
             Name = tm.Name,
             Picture = tm.Picture.ToArray(),
             IsSpeedDial = tm.IsSpeedDial
            };
        ClientInterface.TeamMember someMember = members.First();
    

    Unfortunately, the last line throws a NullReferenceException... anything I do with members throws that. If instead of returning a ClientInterface.TeamMember I simply do a

     

     

    var members = from tm in context.TeamMembers
            where tm.TeamId == myTeam.TeamId
            select tm;
    

    then I have no trouble working with members. Of course I could iterate through the list and generate the custom business object from reach, but that seems way too inefficient, and seeing as things like this: http://stackoverflow.com/questions/5278268/when-selecting-data-for-a-custom-object-from-a-linq-to-sql-left-join-how-can-i-s makes me think it should be possible.. so what am I doing wrong here?

    @edit: almost forgot... if I do a count on members, it returns 5 for both queries... so I know the query is okay.. it's just that I need to turn members into List<ClientInterface.TeamMember> and I don't know how (.toList() doesn't help)


    Wednesday, July 13, 2011 4:00 PM

Answers

  • hi

     

    can you try

     

    List<ClientInterface.TeamMember> members 
     = (from tm in context.TeamMembers
      where tm.TeamId == myTeam.TeamId
      select tm).ToList<ClientInterface.TeamMember>();
    
    
    
    

     

    i'm not the best to explain this, but here goes...

     

    you have two choices:

    1. wrap the LINQ expression in parenthesis, then you have access to the Enumerable extension methods (e.g. ToList<T>()).

    2. otherwise, you will have to access the results of a query within a "foreach" construct. 

    OTHERWISE, i think you get a type mismatch (because the besides the two cases above, the results of the query are not of type ClientInterface.TeamMember, rather they are some more complicated anonymous type)

     

    hope that helps.

     

    thanks

    roger


    thanks, roger
    Wednesday, July 13, 2011 6:12 PM

All replies

  • Hi Stephan,

     

    Just one question.

    Can the tm.Picture be null?

     


    Regards, Peter

    //If a post answers your question, please click "Mark As Answer".
    //Also if a post seems to be helpful, please click "Mark as Helpful" on that post.
    • Edited by Crick3t Thursday, July 14, 2011 2:43 PM
    Wednesday, July 13, 2011 4:49 PM
  •  Of course I could iterate through the list and generate the custom business object from reach, but that seems way too inefficient,


    I disagree. I have a current Silverlight project where I have designed the code to send custom Data Transfer objects where actual entities are too bloated for the consumer's needs and a DTO works fine.

    Note the exception is thrown from the seciton of code because the code is asking an extension method to return the first item of a null list returned from the database, while the second code is returning a null list from the data base and not performing an operation. One should always use the ...OrDefault extension methods when there is a chance to encounter a null list. So change your code to FirstorDefault() instead.

    HTH

     


    William Wegerson (www.OmegaCoder.Com)
    Wednesday, July 13, 2011 5:05 PM
    Moderator
  • hi

     

    can you try

     

    List<ClientInterface.TeamMember> members 
     = (from tm in context.TeamMembers
      where tm.TeamId == myTeam.TeamId
      select tm).ToList<ClientInterface.TeamMember>();
    
    
    
    

     

    i'm not the best to explain this, but here goes...

     

    you have two choices:

    1. wrap the LINQ expression in parenthesis, then you have access to the Enumerable extension methods (e.g. ToList<T>()).

    2. otherwise, you will have to access the results of a query within a "foreach" construct. 

    OTHERWISE, i think you get a type mismatch (because the besides the two cases above, the results of the query are not of type ClientInterface.TeamMember, rather they are some more complicated anonymous type)

     

    hope that helps.

     

    thanks

    roger


    thanks, roger
    Wednesday, July 13, 2011 6:12 PM
  • Yeah... so I suppose I need to write

    Picture = (tm.Picture == null) ? null : tm.Picture.ToArray()
    

    or I'll run into an issue when Picture is null.

    Thursday, July 14, 2011 1:32 PM
  • Roger, this does indeed work. Interestingly, members.GetEnumerator returns an enumerable of type ClientInterface.TeamMember but no other method called on members does.

    Thursday, July 14, 2011 1:41 PM