RIA 域服务中的新实例

发布于 2024-08-08 04:43:09 字数 826 浏览 3 评论 0原文

我正在使用 .NET RIA 服务 7 月预览版在我的 Silverlight 客户端和服务器之间进行通信。在我的服务器上,我有一个托管 Silverlight 应用程序的 ASP.NET 项目。在 ASP.NET 应用程序中,我有一个 Linq-to-Sql 数据模型,其中包含一个名为 Hour 的表。我通过创建 Hour.shared.cs 扩展了 Hour 实体的 3 个属性:

public partial class Hour
{
   public string CustomerName { get; set; }
   public string ProjectName { get; set; }
   public string FullProjectName { get { return this.CustomerName + " - " + this.ProjectName; } }
}

在我的域服务中,我有一个名为 GetHours 的获取方法。由于 Linq 中的设计,我无法显式创建 Hour 实体的新实例,并通过新实体设置新属性的值:

var query = from hours in this.Context.Hours
            select new Hour()
            {
               HourID = hours.HourID,
               ProjectName = "Hello World"
            };

如果我只选择小时,它工作得很好,但我需要如何设置 ProjectName 和 CustomerName 。

关于如何解决这个问题有什么想法吗?

I'm using .NET RIA Services July Preview to communicate between my Silverlight client and my server. On my server I have an ASP.NET project hosting the Silverlight application. In the ASP.NET application I have a Linq-to-Sql DataModel with a table called Hour. I have extended the Hour entity with 3 properties by creating Hour.shared.cs:

public partial class Hour
{
   public string CustomerName { get; set; }
   public string ProjectName { get; set; }
   public string FullProjectName { get { return this.CustomerName + " - " + this.ProjectName; } }
}

In my domainservice I have a get-method called GetHours. Due the design in Linq, I cannot explicit create a new instance of the Hour entity and through the new entity set the values of my new properties:

var query = from hours in this.Context.Hours
            select new Hour()
            {
               HourID = hours.HourID,
               ProjectName = "Hello World"
            };

If I just select hours it works just fine but I need to set the ProjectName and CustomerName some how.

Any ideas about how to get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2024-08-15 04:43:09

再次尝试我的答案,上次我愣住了。

可能最简单的方法是使用函数:

public Hour PopulateHour(Hour hour)
{
  hour.CompanyName = "xyz";
  hour.ProjectName = "123";
  return hour;
}

然后您可以在 linq 查询中使用它:

var query = from hour in this.Context.Hours
            select PopulateHour(hour);

如果您已经将 CompanyId 和 ProjectId 作为 Hour 类中的属性,则可以使此操作变得更容易。

Trying my answer again, last time SO froze on me.

Probably the easiest way to do this would be using a function:

public Hour PopulateHour(Hour hour)
{
  hour.CompanyName = "xyz";
  hour.ProjectName = "123";
  return hour;
}

Then you can use this in your linq query:

var query = from hour in this.Context.Hours
            select PopulateHour(hour);

One thing that would make this easier would be if you already had the CompanyId and ProjectId as properties in the Hour class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文