WinForms Combobox DataSource Class with sub items - combobox

I have two classes getting from json request
public class User
{
public string id { get; set; }
public string name { get; set; }
}
public class PointUser
{
public int PointUserId { get; set; }
public string userId { get; set; }
public User user { get; set; }
public string notes { get; set; }
}
I bind list object of class PointUser to combobox. How can I display sub class User field in my combobox?
cmbPointUser.DataSource = ObjListPointUsers;
cmbPointUser.ValueMember = "PointUserId";
cmbPointUser.DisplayMember = "?????"; // fild "name" from sub class User

You have two options:
1) override the ToString() method in the PointUser class and return the sub property User.Name. Do not define the property DisplayMember.
2) create a property in the class PointUser and return the sub property User.Name. After that call this property in the DisplayMember.

Related

DevExpress LookUpEdit Column Binding To Navigation Property

Good day!
I'm having an issue with the DevExpress LookUpEdit I can't figure out what the problem is.
I'm use Entity Framework list as a datasource.
public partial class provider_scheme : BaseEntity
{
public provider_scheme()
{
}
public int Provider_Scheme_RowID { get; set; }
public int Currency_RowID { get; set; }
public string Provider_Scheme_Name { get; set; }
public virtual currency currency { get; set; }
}
public partial class currency : BaseEntity
{
public currency()
{
provider_scheme = new HashSet<provider_scheme>();
}
public int Currency_RowID { get; set; }
public string Currency_ISOCode { get; set; }
public virtual ICollection<provider_scheme> provider_scheme { get; set; }
}
I'm setting the Datasource property of the LookUpEdit to IEnumerable<provider_scheme>, and setting up two column field names in my LookUpEdit. One for 'Provider_Scheme_Name' and one for 'currency.Currency_ISOCode'. But for some reason only the 'Provider_Scheme_Name' column values are showing. I've also checked and the 'currency' navigation property is being loaded.
Thanks in advance for your help
A bit late for an answer, but you might consider using the GridLookupEdit control instead. It permits adding all the columns you want

AutoMapper with Array and JsonApiSerializer.JsonApi.Relationship

I have an AppService solution with the following Classes and i want to map from the SourceObject to the DestinationObject
Source Classes
public class SourceObject
{
public string Id { get; set; }
public string Name { get; set; }
public JsonApiSerializer.JsonApi.Relationship<SourceChildObject[]> childObjects { get; set; }
}
public class SourceChildObject
{
public string Id { get; set; }
public string type { get; set; }
}
Destination Classes
public class DestinationObject
{
public string Id { get; set; }
public string Name { get; set; }
public JsonApiSerializer.JsonApi.Relationship<DestinationChildObject[]> childObjects { get; set; }
}
public class DestinationChildObject
{
public string Id { get; set; }
public string type { get; set; }
}
Auto mapper is setup in the sartup class
services.AddAutoMapper(typeof(EntityMappingProfile));
And my mapping class loos like this
public class EntityMappingProfile : Profile
{
public EntityMappingProfile()
{
CreateMap<SourceObject, DestinationObject>();
CreateMap<Relationship<SourceChildObject[]>, Relationship<DestinationChildObject[]>>();
}
}
When i execute the solution all fields are mapped apart form the array field of type JsonApiSerializer.JsonApi.Relationship. The destination field is null. What am i doing wrong?
You forgot about creating a map between SourceChildObject and DestinationChildObject. Add this line to your EntityMappingProfile.
CreateMap<SourceChildObject, DestinationChildObject>();
And one more thing, when you are mapping generic types, you can enable mapping for all types with:
CreateMap(typeof(Relationship<>), typeof(Relationship<>));
instead of creating a map with concrete use case of a generic type.

Entity Framework Core - How to include/populate a navigation property with custom(1-to-1) query in EF?

How to include/populate a navigation property with custom(1-to-1) query in EF?
e.g.
public class Item {
public int Id { get; set; }
public string Name { get; set; }
[ForeignKey("Id")]
public ItemCost LatestCost {get; set; }
}
public class ItemCost {
public int Id { get; set; }
public DateTime From { get; set; }
public DateTime? To { get; set; }
public decimal Cost { get; set; }
}
Goal is to populate the LatestCost property of the Item with it's latest cost from ItemCosts. How is this being accomplished with EF or what's your take on this?
Is it possible to do a custom query within .Include/.ThenInclude methods?
e.g.
.ThenInclude(a => { a.LatestCost = (from a _db.ItemCosts
where... select a).SingleOrDefault() })...
You could use a virtual get-only property. Your nav property should really be an ICollection<ItemCost>. In this example I'm assuming the Id property in the ItemCost class is the id of the related Item, but it's not clear. Tip: using nameof(property) instead of hard-coding the property name will allow the compiler to catch errors with the name if you were to change it for some reason. The [NotMapped] attribute tells Entity Framework to not try and map the property to a database field.
public class Item {
public int Id { get; set; }
public string Name { get; set; }
public ICollection<ItemCost> ItemCosts {get; set; }
[NotMapped]
public virtual ItemCost LatestCost
{
get
{
return ItemCosts.OrderByDescending(x => x.From).FirstOrDefault();
}
}
}
public class ItemCost {
public int Id { get; set; }
public DateTime From { get; set; }
public DateTime? To { get; set; }
public decimal Cost { get; set; }
[ForeignKey(nameof(Id))]
public virtual Item Item { get; set; }
}

How to change the content of ListBox when clicked on ListBoxItem?

I have a ListBox and I whant to bind it to a generic list with items of my type (SubscribeUnit).
List<SubscribeUnit> Subscribes = new List<SubscribeUnit>();
where
class SubscribeUnit
{
public string Title { get; set; }
public string Link { get; set; }
public string Description { get; set; }
public Dictionary<FeedUnit, bool> Feeds = new Dictionary<FeedUnit, bool>();
}
class FeedUnit
{
public string Title { get; set; }
public string Link { get; set; }
public string PubDate { get; set; }
public string Descr { get; set; }
}
When I click on ListBoxItem (that displays only the 'Title' property), the ListBox should change the content. To be exact it should now store (only the 'Title' property) FeedUnit's from Dictionary<FeedUnit, bool> Feeds = new Dictionary<FeedUnit, bool>();
How can I do that? Any help appriciated...
Create a data template which changes itself when the ListBoxItem.IsSelected property becomes true using a trigger.

Sending custom DTO via ria services

Hi I have class to sen via ria service.
class look like
[DataContract]
public partial class AttributeNode
{
[DataMember]
[Key]
public int Uid { get; set; }
public AttributeNode()
{
this.Children = new List<String>();
}
private String text;
[DataMember]
public String Text
{
get
{
return text;
}
set
{
text = value;
this.Uid = text.GetHashCode();
}
}
[DataMember]
[Include]
[Association("AttributeNode_AttributeNode", "Uid", "Uid")]
public List<AttributeNode> Children { get; set; }
public void AddChild(AttributeNode child)
{
this.Children.Add(child);
}
}
The problem is that when I recive object to client it's not ok. It always as a Children contain itself. Problem is on List of same type. Help?
Tnx!!
I suppose this is some kind of parent-child tree structure.
The Association tag is used to say "this key" and "other key".
Your AttributeNode class needs a Id property to tell which its parent.
You'd need
[Key]
public int Uid { get; set; }
public int ParentUid { get; set; }
[Include]
[Association("AttributeNode_AttributeNode", "Uid", "ParentUid")]
public List<AttributeNode> Children { get; set; }

Resources