如何使用 WCF RIA 服务准备数据以在 silverlight 图表上显示实体框架

发布于 2024-09-01 01:37:14 字数 680 浏览 3 评论 0原文

我使用 WCF RIA 服务和实体框架来构建一个简单的应用程序,它可以显示和更新有关学校课程的数据。这是通过遵循 Microsoft 教程完成的。现在我想要一个图表,显示关键阶段有多少课程的计数。

示例:

关键阶段 3 - 20 门课程
关键阶段 4 - 32 门课程
关键阶段 5 - 12 门课程

显示在任何形式的图表上。我在 XAML 中将数据绑定到图表没有问题。我的问题是我不知道如何正确地将数据转换为该格式。生成的 CRUD 方法是基本的。

我对可能的方法有一些想法,但不知道哪个是正确的,它们是:

  1. 在 SQL Server 中创建一个视图并将其映射到实体数据模型中的单独实体。自动为此生成新的 CRUD 方法。

  2. 使用 .Select() .Distinct() 等自定义现有 DomainService 中的读取方法。不太了解这种语法 Labda 表达式/LINQ???它是什么?有什么好的快速入门吗?

  3. 创建一个新类来仅存储所需的数据,并为其创建一个读取方法。尝试过此操作,但不知道如何在实体模型中没有匹配实体的情况下使其工作。

  4. 我不知道的事情。

我对此非常陌生,并且正在努力解决这些概念,因此如果我错过了有用的博客或文档,请随时向我指出它们。但我不确定目前在搜索中使用的术语。

I've used WCF RIA services with Entity Framework to build a simple application which can display and updates data about school courses. This was done by following the Microsoft tutorials. Now I would like to have a chart which shows a count for how many courses are on a key stage.

Example:

Key Stage 3 - 20 courses
Key Stage 4 - 32 courses
Key Stage 5 - 12 courses

Displayed on any form of chart. I have no problem binding data to the chart in XAML. My problem is that I do not know how to correct way of getting the data into that format. The generated CRUD methods are basic.

I have a few thoughts about possible ways, but don't know which is correct, they are:

  1. Create a View in SQL server and map this to a separate Entity in the Entity Data Model. Generating new CRUD methods for this automatically.

  2. Customise the read method in the existing DomainService using .Select() .Distinct() etc. Don't know this syntax very well labda expressions/LINQ??? what is it? Any good quickstarts on it?

  3. Create a new class to store only the data required and create a read method for it. Tried this but didn't know how to make it work without a matching entity in the entity model.

  4. Something I am not aware of.

I'm very new to this and struggling with the concepts so if there are useful blogs or documentation I've missed feel free to point me towards them. But I'm unsure of the terminology to use in my searches at the moment.

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

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

发布评论

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

评论(3

一种方法是构建模型类。模型是代表您希望显示的数据的类。例如,我可能有一个包含 10 个字段的表,但我只需要显示 2 个字段。使用这两个属性创建一个模型并从数据层返回该模型。

您可以使用实体框架将数据泵入新类,如下所示

模型类:

public class Kitteh
{
    public string Name { get; set; }  
    public int Age { get; set; }
}

实体查询:

public Iqueryable<Kitteh> getKittehz
{
    var result = from x in Data.TblCats
                 select new Kitteh  
                 {
                    Name = x.Name, 
                    Age = x.Age
                 }
    return result;
}

如果您对构建 silverlight 应用程序的最佳实践方法感兴趣,我建议您研究 MVVM 模式。

http://www.silverlight.net/learn/videos/ silverlight-4-videos/mvvm-introduction/

http://www.silverlight.net/learn/tutorials/silverlight-4/using-the-mvvm-pattern-in-silverlight-applications/

One way to is to build a model class. A model is a class that represents the data you wish to display. For example i might have a table with 10 fields but i only need to display 2. Create a model with these two properties and return that from your data layer.

you can use entity framework to pump data into a new class like so

Model Class:

public class Kitteh
{
    public string Name { get; set; }  
    public int Age { get; set; }
}

Entity Query:

public Iqueryable<Kitteh> getKittehz
{
    var result = from x in Data.TblCats
                 select new Kitteh  
                 {
                    Name = x.Name, 
                    Age = x.Age
                 }
    return result;
}

If you are interested in the best practices approach to building silverlight applications I would suggest you research the MVVM pattern.

http://www.silverlight.net/learn/videos/silverlight-4-videos/mvvm-introduction/

http://www.silverlight.net/learn/tutorials/silverlight-4/using-the-mvvm-pattern-in-silverlight-applications/

半葬歌 2024-09-08 01:37:14

我正在尝试类似的作品。

我会告诉你我将要使用的方法,也许可以帮助你。

我将在 silverlight 项目中创建一个类来描述 ChartItem:它将有 2 个字符串属性:Key 和 Value。

然后创建一个集合对象...在您的情况下,这可能是一个具有 Dictionary类型属性的类。 myCollection...ObservableCollection; myCollection

下一步是对从服务器返回的数据执行 ForEach 循环并添加到您的集合中。

myCollection.Add(new chartItem{ Key= "Key Stage 3", Value = "20 Courses" });
myCollection.Add(new chartItem{ Key= "Key Stage 4", Value = "60 Courses" });
myCollection.Add(new chartItem{ Key= "Key Stage 5", Value = "10 Courses" });

...如果您仍在寻找答案,请关注更多内容

I am attempting a similar piece of work.

I will tell you the approach I am going to use and maybe that can help you.

I am going to create a class in the silverlight project to describe the chartItem: It will have 2 string properties : Key and Value.

Then create a collection object...In your case, this could be a class that has one property of type Dictionary<string,string> myCollection... or ObservableCollection<ChartItem> myCollection

The next step is to do a ForEach loop on the data coming back from the server and Add to your Collection.

myCollection.Add(new chartItem{ Key= "Key Stage 3", Value = "20 Courses" });
myCollection.Add(new chartItem{ Key= "Key Stage 4", Value = "60 Courses" });
myCollection.Add(new chartItem{ Key= "Key Stage 5", Value = "10 Courses" });

... more to follow if you are still looking for an answer

猫腻 2024-09-08 01:37:14

没有简单的方法可以在实体框架中包含视图,因为它不允许在没有“键”(PrimaryKey)的情况下包含任何表/视图,这将导致更多的工作,因为您必须在 EDMX 中手动映射视图,然后映射键等 。

现在我们找到了另一种方法,

  1. 在数据库中创建名为 ChartItems 的视图
  2. 创建 LinqToSQL 文件 ViewDB
  3. 在 ViewDB 中拖动视图 ChartItems
  4. 在 RIA 域服务类中创建 ChartItem[] GetChartItems 方法,如下所示
public ChartItem[] GetChartItems(..parameters...){
   ViewDB db = new ViewDB();
   return db.ChartItems.Where(...查询映射...).ToArray();
}

RIA 域服务类可以包含您可以使用参数直接从客户端调用的任意方法。就像调用 Web 服务一样简单。并且您必须返回一个数组,因为 IQueryable 在某些情况下可能工作也可能不工作,但我们更喜欢 Array。您可以尝试 IQueryable,但它可能无法针对 linq to SQL 正常工作。

There is no easy way to include Views in Entity Framework as it does not allow any table/view to be included without "Key" (PrimaryKey) which will cause more efforts as you will have to map view manually in EDMX and then map keys etc.

Now we have found out an alternative approach,

  1. Create View called ChartItems in your DB
  2. Create LinqToSQL file ViewDB
  3. Drag View ChartItems in ViewDB
  4. Create ChartItem[] GetChartItems method in your RIA Domain Service Class as follow
public ChartItem[] GetChartItems(..parameters...){
   ViewDB db = new ViewDB();
   return db.ChartItems.Where(...query mapping...).ToArray();
}

RIA Domain Service Class can contain any arbitrary method that you can directly invoke from client with parameters. It is as simple as calling a web service. And you have to return an array because IQueryable may or may not work in some cases, but we prefer Array. You can try IQueryable but it may not work correctly against linq to SQL.

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