域服务返回自定义类结果

时间:2013-12-08 10:54:18

标签: c# wcf silverlight-5.0 wcf-ria-services ria

我希望在调用方法中返回两种类型,我知道我们不能在WCF RIA中使用out或ref。

我创建了一个自定义类来获取这样的必要信息:

    [DataContract]
    public class Web_GetPrivilegesResult  {
        [Include]
        [DataMember]
        public List<tblPrivilege> ResultList { get; set; }

        [DataMember]
        public Guid? ParentGroupID { get; set; }
    }

    [Invoke]
    public Web_GetPrivilegesResult Web_GetPrivileges(Guid id, bool isSuperAdmin, bool isEnable, bool returnAccessListIfDisbaled) {
        ...
    }

在客户端(silverlight应用程序)调用该函数时,它仅返回ParentGroupID - ResultList未重新启动。我怎么能纠正这个?

更新

上次更改的代码:

    [DataContract]
    public class Web_GetPrivilegesResult  {
        //[System.ComponentModel.DataAnnotations.Composition]
        //[Include]
        [DataMember]
        public List<tblPrivilege> Result { get; set; }

        [DataMember]
        public Guid? ParentGroupID { get; set; }
    }

    //[Query]

    [Invoke]
    public Web_GetPrivilegesResult Web_GetPrivileges(Guid id, bool isSuperAdmin, bool isEnable, bool returnAccessListIfDisbaled) {
        tblUsersAndGroup usersAndGroup = new tblUsersAndGroup() { ID = id, IsSuperAdmin = isSuperAdmin, IsEnable = isEnable };

        Guid? parentGroupID;
        List<tblPrivilege> result = GetPrivileges(usersAndGroup, out parentGroupID, returnAccessListIfDisbaled, this.DataContext);

        //System.Data.Linq.DataLoadOptions options = new System.Data.Linq.DataLoadOptions();
        //options.LoadWith<Web_GetPrivilegesResult>(q => q.Result);
        //this.DataContext.LoadOptions = options;

        //List<Web_GetPrivilegesResult> res = new List<Web_GetPrivilegesResult>();
        //res.Add(new Web_GetPrivilegesResult() { ParentGroupID = parentGroupID, Result = result });


        return new Web_GetPrivilegesResult() { ParentGroupID = parentGroupID, Result = result };

    }

感谢

1 个答案:

答案 0 :(得分:1)

由于使用了[Include]属性,因此在查询方法中,必须确保在查询中使用包含方法实际加载关联实体。

您需要使用查询中的ResultList方法显式请求(使用LINQ to Entities)从数据库中检索每个Web_GetPrivilegesResult实体上Include属性的数据,如此:

public IQueryable<Web_GetPrivilegesResult> GetPrivilegesResult()
{
    return this.ObjectContext.Web_GetPrivilegesResult.Include("ResultList");
}

<强>更新

只需在.Include("ResultList")方法的return末尾添加Web_GetPrivileges(Guid id, bool isSuperAdmin, bool isEnable, bool returnAccessListIfDisbaled)即可!

<强> UPDATE2:

您可以看到thisthis与Linq2SQL相当于Include()