Click here to Skip to main content
15,910,981 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
I need to return this query result from web service, how can I do that ?
notice : without looping on result data or filling another datatable.



C#
var query = from usr in BCM.Users
                    join usrgrp in BCM.UsersGroups
                    on usr.userID equals usrgrp.userID
                    where usrgrp.groupID == ID
                    select new { usr.firstName, usr.FamilyName, usr.userName,
                    usrgrp.Group.groupName, usrgrp.Group.groupDescription };
Posted
Comments
Kim Togo 23-May-11 9:21am    
Is it "query" that you want to return as an array ?
M.Behery 23-May-11 10:37am    
yes its a query but I need to return the result as a class or an serializable object.

1 solution

Hello,

One possible way would be that you define a data-entity which you can fill-in in your select statement:

The entity:
C#
public class UserEntity
{
    public string Firstname { get; set; }
    public string Familyname { get; set; }
    public string Username { get; set; }
    public string GroupName { get; set; }
    public string GroupDescription { get; set; }
}


The select block:
var query = from usr in BCM.Users
	join usrgrp in BCM.UsersGroups
	on usr.userID equals usrgrp.userID
	where usrgrp.groupID == ID
	select new UserEntity {
		Firstname = usr.firstName, Familyname = usr.FamilyName, Username = usr.userName,
		GroupName = usrgrp.Group.groupName, GroupDescription = usrgrp.Group.groupDescription
	};


Then you could return the value as:
return query.FirstOrDefault();

(This returns a strong-typed UserEntity object)
or
return query.ToList();

(This returns a strong-typed List<UserEntity>)

Another way would be that you directly convert the resulting anonymous object to a Xml or Json representation (depends on what you can use as output of your webservice) and returns the serialized string.


Hope this helps.

Best regards and happy coding,
Stops
 
Share this answer
 
v2
Comments
M.Behery 23-May-11 10:46am    
thanks Stops for this solution of strong-typed class UserEntity
but it will be complicated when I am using DB of 100 tables and there are a lot of queries which combine more than one entity in it. so now I will make for each query its own entity class in my code or what ?
Christoph Keller 23-May-11 10:55am    
Yes, that's right, anonymous objects are only valid in the scope they're created so you need to fill the data into any other object / structure if you want to do further processing / returning the data.

Perhaps it is easier if you have a consistent datasource / DB with correct primary keys and correct relations, that Linq to SQL can give you the possibility to access for example the UserGroups by accessing User.UserGroups..

But I think this would go beyond the scope of this question :)

Hope this helps.

Best regards,
Stops
Sandeep Mewara 23-May-11 11:02am    
My 5!
Christoph Keller 23-May-11 11:08am    
Thank you! :)
Wonde Tadesse 23-May-11 23:03pm    
My 5:). Well explained answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900