在Web服务中使用LINQ to SQL [英] usign LINQ to SQL in web service
问题描述
我需要从Web服务返回此查询结果,该怎么办?
注意:请勿循环访问结果数据或填充另一个数据表.
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.
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 };
推荐答案
您好,
一种可能的方式是定义一个数据实体,您可以在选择语句中填写该数据实体:
实体:
Hello,
One possible way would be that you define a data-entity which you can fill-in in your select statement:
The entity:
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();
(这将返回一个强类型的UserEntity
对象)
或
(This returns a strong-typed UserEntity
object)
or
return query.ToList();
(这将返回一个强类型List<UserEntity>
)
另一种方法是直接将生成的匿名对象转换为Xml或Json表示形式(取决于您可以用作Web服务的输出),并返回序列化的字符串.
希望这会有所帮助.
致以诚挚的问候,祝您编程愉快,
停止
(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
这篇关于在Web服务中使用LINQ to SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!