开发者

How to prepare data for display on a silverlight chart using WCF RIA Services + Entity Framework

开发者 https://www.devze.com 2022-12-29 06:26 出处:网络
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

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.


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/


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消