I had previously asked the same question - but probably not very clearly. So I thought I would repost with a sample.
My basic problem is that I have an EntitySet which contains data falling into multiple categories.
I am looking for the best way to display this data in it's seperate categories without changing the underlying database.
I have created a small sample for the purpose.If you open up the solution and run it you should see a screen with a name at the top and then three list boxes (the screen aint pretty but thats not the point!)
Each list box contains three entries for each user, what I need to achieve (based on the data currently in the database) is one record per list box per user.
If I then add, say, another breakfast for user 1 that would be two entries in the breakfast list and one each in lunch and dinner etc.
Hopefully when you see it it will make sense. If anyone has any suggestions on how I can achieve what I am trying to do here please post.
Apologies again if I am just being dense and missing something real obvious!
Nic, I have already provided you three solutions, the first one you already knew, the second doesn't exactly match your layout, and you seem to be trying real hard to completely ignore the third one. Polluting the message board by double posting isn't going
to help.
For reference:
Use multiple DomainDataSources
Use a DataGrid or ItemsControl (understood, this will not work)
Thank you for your reply and yes you have already provided me with three solutions - thank you for that - however I have been unable to make any of them fit what I am trying to do. I repackaged/reposted the question as I felt my original question was not
succinct in what I was trying to achieve.
As someone who has worked in the IT industry for 15+ years I am well aware of how annoying it is to find repeated information, on the internet, when trying to solve a problem. However, whilst you may be justified in saying that I am currently being a bit
thick or slow on the uptake on this particular issue - a polluter of forums I am not. So yes, I will revisit your third suggestion, and if it resolves the problem it will be marked as the answer.
In the interim, anyone who may have had this issue themselves or who has any suggestions please feel free to respond.
I don't know if it benefits you much, but I like Colin's third suggestion as well. A few simple queries and you'd be on your way. This may not compile, but it should give you a place to start.
public partial class MyDomainContext
{
// public EntityList<MyEntity> MyEntities { get; set; etc; }public IEnumerable MyFilteredEntities
{
get { return this.MyEntities.Where(e => e.MyType == MyTypes.Type1); }
}
public partial OnCreated()
{
this.MyEntities.CollectionChanged += this.MyEntities_CollectionChanged;
}
private MyEntities_CollectionChanged(object sender, CollectionChangedEventArgs e)
{
this.RaisePropertyChanged("MyFilteredEntities");
}
}
As someone who has worked in the IT industry for 15+ years I am well aware of how annoying it is to find repeated information, on the internet, when trying to solve a problem. However, whilst you may be justified in saying that I am currently being a bit thick
or slow on the uptake on this particular issue - a polluter of forums I am not. So yes, I will revisit your third suggestion, and if it resolves the problem it will be marked as the answer.
For future reference, let me point out two things. First, expect responses to be slow over US holiday weekends like this last weekend. That is why you hadn't gotten any more responses, not because nobody noticed your post. Second, this forum does allow you
to modify your posts so if you think that your original post (or even the subject) need to be modified to reduce confusion you as the post originator can make that change without posting a whole new threads. Those of us answering questions looks for the ?
icon to tell us the thread hasn't been answered, "bumping" the thread to keep it active is acceptable.
Also, you aren't being thick or slow. The piece of information I was originally missing was that you needed multiple of each meal type for a single person. I got that they could have any combination of breakfast, lunch, and/or dinner but multiples. That
invalidated solution 2.
Like kylemc, I am still thinking that Colins third solution (shown here)
namespace The.Correct.Namespace
public partial class Person
{
public Meal Breakfast
{
get
{
return (from pm in PersonMeals
where pm.Meal.MealTypeId = 1
select pm.Meal).SingleOrDefault();
}
}
public Meal Lunch
{
get
{
return (from pm in PersonMeals
where pm.Meal.MealTypeId = 2
select pm.Meal).SingleOrDefault();
}
}
public Meal Dinner
{
get
{
return (from pm in PersonMeals
where pm.Meal.MealTypeId = 3
select pm.Meal).SingleOrDefault();
}
}
}
Is probably the way to go. However I am having a devil of a time trying to expose a new property of say Breakfasts to the client.
I have added a new class file in the services folder of the web project. In it I have the following.
public partial class People
{
[global::System.Runtime.Serialization.DataMemberAttribute()]
public int TestProperty
{
get { return 57; }
}
//[global::System.Data.Objects.DataClasses.EdmRelationshipNavigationPropertyAttribute("MealSampleModel", "FK_tblMeals_tblPeople", "tblMeals")]
[global::System.Xml.Serialization.XmlIgnoreAttribute()]
[global::System.Xml.Serialization.SoapIgnoreAttribute()]
[global::System.Runtime.Serialization.DataMemberAttribute()]
public EntityCollection Breakfasts
{
get
{
return null;
}
}
}
So OK there is no implementation yet for the Breakfasts property but it is not available at runtime. My TestProperty property on the other hand is.
I am assuming it is something to do with serialization but I have been through many, many variations of attributes on the property etc. but have had no luck.
The new properties should be added client side, not server side. They are just filters against the full meals list. That latest batch of sample code above was all set to be inserted into your client side project.
OK, I finally checked my code from above and it did need Association attributes added but other than that it works perfectly. You should be able to take the code in that post, paste it into your project, modify your bindings, and it will work.
Brilliant - thats got it. I had tried yesterday putting the methods on the client side but it hadn't worked for some reason. Possibly got the wrong namespace.
Anyway my final code looks like this. In a class file on the client side
public partial class People
{
public List Breakfasts
{
get { return (GetMealListByType(1)); }
}
public List Lunches
{
get { return (GetMealListByType(2)); }
}
public List Dinners
{
get { return (GetMealListByType(3)); }
}
public List Snacks
{
get { return (GetMealListByType(4)); }
}
private List GetMealListByType(int mealTypeId)
{
return (from m in this.tblMeals
where m.tblMealType.mealTypeId == mealTypeId
select m).ToList();
}
}
What I then found was that the first record bound did not show the filtered data, once I moed to the next record the filter worked and if I move back again to the original record the filter work. To overcome this I moved the create datasource function and the call to assign the datasource to the data form into the Context_Loaded handler. It needed a boolean flag so that these two lines were only called once. It looks like this.
Thanks for all your help Colin. This has been a really useful exercise for me in terms of seeing how a RIA services based app is constructed. I marked your original answer as the correct one. If I had been a little further down the line with my SL3 and
RIA experimentation perhaps I would have realised that that was the answer sooner!
Nic Oughton
Member
9 Points
13 Posts
Displaying Multiple Data Lists From One Source Data EntitySet
Jul 06, 2009 09:25 AM | LINK
I had previously asked the same question - but probably not very clearly. So I thought I would repost with a sample.
My basic problem is that I have an EntitySet which contains data falling into multiple categories.
I am looking for the best way to display this data in it's seperate categories without changing the underlying database.
I have created a small sample for the purpose.If you open up the solution and run it you should see a screen with a name at the top and then three list boxes (the screen aint pretty but thats not the point!)
Each list box contains three entries for each user, what I need to achieve (based on the data currently in the database) is one record per list box per user.
If I then add, say, another breakfast for user 1 that would be two entries in the breakfast list and one each in lunch and dinner etc.
Hopefully when you see it it will make sense. If anyone has any suggestions on how I can achieve what I am trying to do here please post.
Apologies again if I am just being dense and missing something real obvious!
The zip of my sample solution can be found here
Nic
ColinBlair
All-Star
28591 Points
4834 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 06, 2009 02:34 PM | LINK
Nic, I have already provided you three solutions, the first one you already knew, the second doesn't exactly match your layout, and you seem to be trying real hard to completely ignore the third one. Polluting the message board by double posting isn't going to help.
For reference:
http://www.RiaServicesBlog.net
ColinBlair on Twitter
MVVM and RIA Services
Nic Oughton
Member
9 Points
13 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 06, 2009 04:26 PM | LINK
Hi Colin,
Thank you for your reply and yes you have already provided me with three solutions - thank you for that - however I have been unable to make any of them fit what I am trying to do. I repackaged/reposted the question as I felt my original question was not succinct in what I was trying to achieve.
As someone who has worked in the IT industry for 15+ years I am well aware of how annoying it is to find repeated information, on the internet, when trying to solve a problem. However, whilst you may be justified in saying that I am currently being a bit thick or slow on the uptake on this particular issue - a polluter of forums I am not. So yes, I will revisit your third suggestion, and if it resolves the problem it will be marked as the answer.
In the interim, anyone who may have had this issue themselves or who has any suggestions please feel free to respond.
Nic
kylemc
Contributor
7231 Points
1146 Posts
Microsoft
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 06, 2009 05:05 PM | LINK
I don't know if it benefits you much, but I like Colin's third suggestion as well. A few simple queries and you'd be on your way. This may not compile, but it should give you a place to start.
Kyle
ColinBlair
All-Star
28591 Points
4834 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 06, 2009 05:19 PM | LINK
This has been checked and bug fixed. It works, just drop it into your Silverlight project and modify your bindings. Snacks have now been added.
using System.Windows.Ria.Data;
using System.ComponentModel.DataAnnotations;
namespace MealSample.Web.Services
{
public sealed partial class People
{
private EntityCollection<Meals> _breakfasts;
private EntityCollection<Meals> _lunches;
private EntityCollection<Meals> _dinners;
private EntityCollection<Meals> _snacks;
[Association("People_Breakfasts", "personId", "personId")]
public EntityCollection<Meals> Breakfasts
{
get
{
if ((this._breakfasts == null))
{
this._breakfasts = new EntityCollection<Meals>(this, "Breakfasts", this.FilterBreakfast, this.AttachtblMeals, this.DetachtblMeals);
}
return this._breakfasts;
}
}
[Association("People_Lunchs", "personId", "personId")]
public EntityCollection<Meals> Lunches
{
get
{
if ((this._lunches == null))
{
this._lunches = new EntityCollection<Meals>(this, "Lunches", this.FilterLunch, this.AttachtblMeals, this.DetachtblMeals);
}
return this._lunches;
}
}
[Association("People_Dinners", "personId", "personId")]
public EntityCollection<Meals> Dinners
{
get
{
if ((this._dinners == null))
{
this._dinners = new EntityCollection<Meals>(this, "Dinners", this.FilterDinner, this.AttachtblMeals, this.DetachtblMeals);
}
return this._dinners;
}
}
[Association("People_Snacks", "personId", "personId")]
public EntityCollection<Meals> Snacks
{
get
{
if ((this._snacks == null))
{
this._snacks = new EntityCollection<Meals>(this, "Snacks", this.FilterSnack, this.AttachtblMeals, this.DetachtblMeals);
}
return this._snacks;
}
}
private bool FilterBreakfast(Meals entity)
{
return (entity.personId == this.personId && entity.mealType == 1);
}
private bool FilterLunch(Meals entity)
{
return (entity.personId == this.personId && entity.mealType == 2);
}
private bool FilterDinner(Meals entity)
{
return (entity.personId == this.personId && entity.mealType == 3);
}
private bool FilterSnack(Meals entity)
{
return (entity.personId == this.personId && entity.mealType == 4);
}
}
}
http://www.RiaServicesBlog.net
ColinBlair on Twitter
MVVM and RIA Services
ColinBlair
All-Star
28591 Points
4834 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 06, 2009 05:34 PM | LINK
For future reference, let me point out two things. First, expect responses to be slow over US holiday weekends like this last weekend. That is why you hadn't gotten any more responses, not because nobody noticed your post. Second, this forum does allow you to modify your posts so if you think that your original post (or even the subject) need to be modified to reduce confusion you as the post originator can make that change without posting a whole new threads. Those of us answering questions looks for the ? icon to tell us the thread hasn't been answered, "bumping" the thread to keep it active is acceptable.
Also, you aren't being thick or slow. The piece of information I was originally missing was that you needed multiple of each meal type for a single person. I got that they could have any combination of breakfast, lunch, and/or dinner but multiples. That invalidated solution 2.
http://www.RiaServicesBlog.net
ColinBlair on Twitter
MVVM and RIA Services
Nic Oughton
Member
9 Points
13 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 07, 2009 09:18 AM | LINK
Like kylemc, I am still thinking that Colins third solution (shown here)
So OK there is no implementation yet for the Breakfasts property but it is not available at runtime. My TestProperty property on the other hand is.
I am assuming it is something to do with serialization but I have been through many, many variations of attributes on the property etc. but have had no luck.
Any thoughts?
ColinBlair
All-Star
28591 Points
4834 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 07, 2009 12:42 PM | LINK
The new properties should be added client side, not server side. They are just filters against the full meals list. That latest batch of sample code above was all set to be inserted into your client side project.
http://www.RiaServicesBlog.net
ColinBlair on Twitter
MVVM and RIA Services
ColinBlair
All-Star
28591 Points
4834 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 07, 2009 08:37 PM | LINK
OK, I finally checked my code from above and it did need Association attributes added but other than that it works perfectly. You should be able to take the code in that post, paste it into your project, modify your bindings, and it will work.
http://www.RiaServicesBlog.net
ColinBlair on Twitter
MVVM and RIA Services
Nic Oughton
Member
9 Points
13 Posts
Re: Displaying Multiple Data Lists From One Source Data EntitySet
Jul 07, 2009 09:29 PM | LINK
Brilliant - thats got it. I had tried yesterday putting the methods on the client side but it hadn't worked for some reason. Possibly got the wrong namespace.
Anyway my final code looks like this. In a class file on the client side
What I then found was that the first record bound did not show the filtered data, once I moed to the next record the filter worked and if I move back again to the original record the filter work. To overcome this I moved the create datasource function and the call to assign the datasource to the data form into the Context_Loaded handler. It needed a boolean flag so that these two lines were only called once. It looks like this.
Thanks for all your help Colin. This has been a really useful exercise for me in terms of seeing how a RIA services based app is constructed. I marked your original answer as the correct one. If I had been a little further down the line with my SL3 and RIA experimentation perhaps I would have realised that that was the answer sooner!
Regards,
Nic