【问题标题】:Domain Service Return Custom Class Result域服务返回自定义类结果
【发布时间】:2013-12-08 10:54:17
【问题描述】:

我想将两种类型返回给调用方法,我知道我们不能在 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 };

    }

谢谢

【问题讨论】:

  • 没有返回,因为它是空的?
  • no ResultList 属性未在 silverlight 中生成,我无法访问它

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


【解决方案1】:

由于使用了[Include]属性,在查询方法中,必须确保关联实体是通过查询上的Include方法实际加载的。

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

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

更新:

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

更新 2:

您可以看到 thisthis 的 Linq2SQL 等效于 Include()

【讨论】:

  • 我正在使用 LinqToSqlDomainService,并且我有与实体一起使用的静态函数并获取 tblPrivilege 的结果列表,我该如何使用你说的这种方式?谢谢
  • Include 方法在哪里,我找不到它,如您所见,Include 方法不是 Web_GetPrivilegesResult 类的成员,Web_GetPrivilegesResult 也不是数据上下文实体...
  • 我正在删除 [Include] 并清理并重建解决方案,但我无法访问它,我更新了我的问题,请查看
猜你喜欢
  • 1970-01-01
  • 2011-05-12
  • 1970-01-01
  • 2023-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 2013-12-27
相关资源
最近更新 更多