I have two classes which contain the same fields, however one inherits some properties from somewhere else and the other does not.
I have created a generic list using the class "ZEUS_ResearchStocksHistory" , but then I need to clone all of the fields over to the other list "ZEUS_ResearchStocksHistoryWithExcel". I don't want to have to loop through each field in one list and populate the other, or write some sort of linq join, there must be a faster way?
The reason I can't use the same class in both instances is that when inheriting the ExcelReport function it adds additional fields which I do not want when I display this list in a data grid.
internal class ZEUS_ResearchStocksHistory
{
public String Amendment { get; set; }
public String AmendedBy { get; set; }
public String Sedol { get; set; }
public String Date { get; set; }
}
internal class ZEUS_ResearchStocksHistoryWithExcel : ExcelReport
{
public String Amendment { get; set; }
public String AmendedBy { get; set; }
public String Sedol { get; set; }
public String Date { get; set; }
}
Is this possible?
Thanks
Did you have a look at automapper?
example from codeproject:
CustomerViewItem customerViewItem =
Mapper.Map<Customer, CustomerViewItem>(customer);
Check out Automapper, which is designed to do exactly this. Automapper is up on NuGet.
http://lostechies.com/jimmybogard/2009/01/23/automapper-the-object-object-mapper/
You could do something as simple as:
Mapper.CreateMap<ZEUS_ResearchStocksHistory, ZEUS_ResearchStocksHistoryWithExcel>();
var newObject = Mapper.Map<ZEUS_ResearchStocksHistory, ZEUS_ResearchStocksHistoryWithExcel>(oldObject);
Or, since you said you have a list, you could do:
var newList = oldList.Select(x => Mapper.Map<ZEUS_ResearchStocksHistory, ZEUS_ResearchStocksHistoryWithExcel>(x));
Related
Let's say I have the following:
public class Order
{
public string orderNumber { get; set; }
public string customerName { get; set; }
public bool isRewardsMember { get; set; }
public string transactionNumber { get; set; }
public List<Guid> cart { get; set; }
public CouponResponse GeneratedCoupons = GenerateCoupons(this); <--- How could I do this?
public CouponResponse GenerateCoupons(Order order)
{
string congrats = "Based on this transaction we believe you would like these coupons.";
CouponResponse.orderGuid = order.orderGuid;
CouponResponse.orderNumber = order.orderNumber;
// blah blah blah
}
}
And I wanted to automatically create some kind of string response based on the object on generation. Could I do something like the above and just do something like this.order, or should I just use a constructor and bypass this? I want to try to have this function to be callable in case couponresponse is for whatever reason not created, or simply use this for further data collection later.
Passing an object for 2 properties seems like unnecessary coupling, but you could use the following syntax to make GeneratedCoupons do what you want:
public CouponResponse GeneratedCoupons => GenerateCoupons(this);
I have nested documents such as;
public sealed class CampaignIndexModel : ElasticEntity<Guid>
{
public Guid StoreId { get; set; }
public string Slug { get; set; }
public string SlugKey { get; set; }
public string Title { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public string Condition { get; set; }
public string PreviewImageUrl { get; set; }
public DateTime? StartTime { get; set; }
public DateTime? EndTime { get; set; }
public bool IsPublished { get; set; }
public DateTime CreatedOnUtc { get; set; }
[Nested]
public List<BadgeIndexModel> Badges { get; set; }
}
public class BadgeIndexModel
{
public string Code { get; set; }
public string Name { get; set; }
}
I'd like to query in nested object with multiple values. For example, I need to query which included Code property which are "AD", "NEW". All documents must have badge and their code properties must be "AD" and "NEW". The code properties can be dynamically. Actually I'd like to search list of string in the nested object's code property.
Note that the classes are auto-mapped while creating indexes.
I hope the question is clear, understandable.
Thank you.
UPDATE
As far as I researched Elasticsearch documentations, as below, the query result returns match exactly given badges codes.
q.Bool(b=>b
.Must(x=>x.
Nested(n=>n
.Path(p=>p.Badges)
.Query(qq=>qq
.Term(t=>t
.Field(f=>f.Badges.First().Code.Suffix("keyword"))
.Value(badge))))))
Then, the answer which is marked correct, returns documents which contains badge codes
I know it has been a little while you posted the question. But here you go -- You could do this by creating a Nested Query within which you could filter upon your list and pass this to your search method. Below method shows how this can be done. This takes the list of strings that you want to use as values for codes.
private static QueryContainer BuildNestedQuery(List<string> badgeCodes)
{
// badgeCodes is your list of strings that you want to filter on
return new QueryContainerDescriptor<CampaignIndexModel>()
.Nested(n =>
n.Path(c => c.Badges)
.Query(q => q
.Terms(t => t
.Field(f => f.Badges.FirstOrDefault().Code)
.Terms(badgeCodes.ToArray())
)
)
)
}
This QueryContainer can further be passed to the Search method of the NEST client like something shown below. However, please bear in mind that there could be slight changes in the way you trigger the client's search method depending on how you're doing it, but hooking it to the search method remains more or less the same as shown below.
// replace T with type of your choice
// client is a reference to NEST client
var result = client.Search<T>(
.From(0)
.Size(20)
.Query(q => BuildNestedQuery(badgeCodesList))
// other methods that you want to chain go here
)
I'm trying to create a map between a domain object and viewmodel to support a use case that feels quite common. The fact that I can't find a solution makes me think I'm approaching the problem incorrectly. Here's some psuedo code that represents my source and destination types:
public class DomainClass
{
public NestedDomainClass1 NestedDomainClass1{ get; set; }
}
public class NestedDomainClass1
{
public NestedDomainClass2 NestedDomainClass2 { get; set; }
}
public class NestedDomainClass2
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
public class DomainViewModel
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
public string PropertyC { get; set; }
}
As you can see, DomainViewModel maps perfectly to DomainClass.NestedDomainClass1.NestedDomainClass2. However, for reasons that aren't entirely relevant, I can't simply create a mapping at that level. Instead I have to map two levels higher:
Mapper.CreateMap<DomainObj, DomainViewModel>();
This is unfortunate, as the minute I do this, I can no longer rely on AutoMapper conventions to automagically map similarly named properties, and I end having to write a lot of highly repetitive code:
Mapper.CreateMap<DomainClass, DomainViewModel>().ForMember(dest=>dest.PropertyA, opt=>opt.MapFrom(source=>source.NestedDomainClass1.NestedDomainClass2.PropertyA));
Mapper.CreateMap<DomainClass, DomainViewModel>().ForMember(dest=>dest.PropertyB, opt=>opt.MapFrom(source=>source.NestedDomainClass1.NestedDomainClass2.PropertyB));
I've played with the RecognizeDestinationPrefixes and RecognizeDestinationPostfixes methods in the hopes of getting AutoMapper to "skip" directly to the property I'd like to map from (NestedDomainClass2), but no luck. Any help would be appreciated!
That's because you're trying map between two completely different types. You really need to do something like this:
Mapper.CreateMap<NestedDomainClass2, DomainViewModel>();
Mapper.AssertConfigurationIsValid();
var dvm = Mapper.Map<NestedDomainClass2, DomainViewModel>
(obj.NestedDomainClass1.NestedDomainClass2);
However if you want to, you can hide that detail by defining a TypeConverter. Something like this should work:
public class DomainTypeConverter : TypeConverter<DomainClass, DomainViewModel>
{
protected override DomainViewModel ConvertCore(DomainClass source)
{
return Mapper.Map<NestedDomainClass2, DomainViewModel>
(source.NestedDomainClass1.NestedDomainClass2);
}
}
You can then define your mapping to be something like this:
Mapper.CreateMap<NestedDomainClass2, DomainViewModel>();
Mapper.CreateMap<DomainClass, DomainViewModel>()
.ConvertUsing(new DomainTypeConverter());
Mapper.AssertConfigurationIsValid();
And use it like this:
var dvm = Mapper.Map<DomainClass, DomainViewModel>(dc);
I need to create an XML file using C#.
I am using a class that inherits List that represents a list of computers and later initialize it with values but the serializer doesn't get the attributes for this class, only for its descendants.
this is the class:
public class Computers : List<Computer>
{
[XmlAttribute("StorageType")]
public int StorageType { get; set; }
[XmlAttribute("StorageName")]
public string StorageName { get; set; }
}
public class Computer
{
[XmlAttribute("StorageType")]
public int StorageType { get; set; }
[XmlAttribute("StorageName")]
public string StorageName { get; set; }
public string IPAddress { get; set; }
public string Name { get; set; }
}
The result should look something like this:
<fpc4:Computers StorageName="Computers" StorageType="1">
<fpc4:Computer StorageName="{D37291CA-D1A7-4F34-87E4-8D84F1397BEA}" StorageType="1">
<fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
<fpc4:Name dt:dt="string">Computer1</fpc4:Name>
</fpc4:Computer>
<fpc4:Computer StorageName="{AFE5707C-EA71-4442-9CA8-2A6264EAA814}" StorageType="1">
<fpc4:IPAddress dt:dt="string">127.0.0.1</fpc4:IPAddress>
<fpc4:Name dt:dt="string">Computer2</fpc4:Name>
</fpc4:Computer>
But what I get so far is this:
<fpc4:Computers>
<fpc4:Computer StorageType="1" StorageName="{7297fc09-3142-4284-b2e9-d6ea2fb1be78}">
<fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
<fpc4:Name>Computer1</fpc4:Name>
</fpc4:Computer>
<fpc4:Computer StorageType="1" StorageName="{eab517f6-aca9-4d01-a58b-143f2e3211e7}">
<fpc4:IPAddress>127.0.0.1</fpc4:IPAddress>
<fpc4:Name>Computer2</fpc4:Name>
</fpc4:Computer>
</fpc4:Computers>
As you can see the Computers node which is the parent node doesn't get the attributes.
Do you guys have a solution?
XmlSerializer treats lists completely separate to leaf nodes; properties on lists do not exist - it is just a collection of the contained data. A better approach would be:
public class Computers {
private readonly List<Computer> items = new List<Computer>();
[XmlElement("Computer")]
public List<Computer> Items { get { return items; } }
[XmlAttribute("StorageType")]
public int StorageType { get; set; }
[XmlAttribute("StorageName")]
public string StorageName { get; set; }
}
This is an object that has a set of computers and has two attributes - but is not a list itself. The use of XmlElementAttribute for the list flattens the nesting as desired. Note that I have omitted namespaces for convenience.
Inheriting from a list (with an aim of adding members) will not work well, not just for XmlSerlaizer, but for a wide range of serializers and binding frameworks.
There seems to be many questions relating to binding on a DataTable, but none that I could find touch on my exact situation.
I'm currently ajax binding on a list of objects like such:
public class MyObject
{
public string Name { get; set; }
public string Something1{ get; set; }
public string Something2{ get; set; }
public string Something3 { get; set; }
public MyObjectMyObject2 { get; set; }
}
public class MyObject2
{
public string Color { get; set; }
public string Something4 { get; set; }
}
[GridAction]
public ActionResult GetData()
{
var data = QueryDatabaseAndInstantiateAListOfMyObjects();
return View(new GridModel(data));
}
And with a view like such:
<%= Html.Telerik().Grid<MyObject>()
.DataBinding(dataBinding => dataBinding.Ajax().Select("GetData", new { action = "GetData" }))
.Name("Grid1")
.Columns(columns =>
{
columns.Bound(o => o.Name).Title("Name1");
columns.Bound(o => o.MyObject2.Color).Title("Color");
columns.Bound(o => o.Something1).Hidden(true);
columns.Bound(o => o.Something2).Hidden(true);
columns.Bound(o => o.Something3).Hidden(true);
columns.Bound(o => o.MyObject.Something4).Hidden(true);
})
%>
This works great and all as I'm able to sort, group, and all the above.
My situation is I have many properties on MyObject and some edges cases are popping up that are yielding a couple megabytes of response data. The reason being there are many many hidden columns that are situational dependent that a user can right-click to show. The problem is, data for all these extra hidden columns are included in the response data even when they're not used per say. And since the act of grouping, un-grouping, showing and hiding columns fetches for data anyways, why does all the extra data have to come with it?
If I could have only the data returned that is necessary to populate the visible columns plus say a couple that I could mark somehow with a custom attribute, that would immensely help cut back on the size of the returned data.
So I took to converting my list of objects to a DataTable that I could then conditionally add columns + data for and then feed that to the GridModel. This worked well up until trying to group by a column that is in a nested object such as o.MyObject2.Color.
I run into this exception:
Property with specified name: MyObject2.Color cannot be found on type: System.Data.DataRowView
I guess this makes sense, but how do I overcome this? When I use Object Shredder, it sets each property of MyObject loosely typed such as ["Name"] as a string and ["MyObject2"] as a MyObject2. But everything past ["MyObject2"] is strongly typed: (dataRow["MyObject2"] as MyObject2).Color. And this is where is gets over my head.
Is there another way to overcome my initial issue of all that extra data being sent that isn't used? Or, is there any advice with the DataTable bit? I've also tried converting the DataTable to a IEnumerable with no such luck. The serialized Json is quite empty. I've also tried flattening all nested objects such as having datarow["MyObject2.Color"] as string, but this wreaks havok when referencing this column in JavaScript so I had to go with an underscore delimiter ["MyObject2_Color"] but this really screws up binding Columns in the UI. There has to be a way!
I don't see any reason to bring back your full object just to show Color and Something4. Try flattening it out. Something like this where you just assign the values of Color and Something4 to the properties of the model.
public class MyObject
{
public string Name { get; set; }
public string Something1{ get; set; }
public string Something2{ get; set; }
public string Something3 { get; set; }
public string Something4 { get; set; }
public string Color { get; set; }
}
[GridAction]
public ActionResult GetData()
{
var data = QueryDatabaseAndInstantiateAListOfMyObjects();
data.Something4 = MyObject2.Something4;
data.Color = MyObject2.Color;
return View(new GridModel(data));
}
This is the true Answer, but I'm giving credit to Alfalfa for the idea.
I continue along with the DataTable idea, but use getters to expose each nested object property. It's a bit conveluded and brittle, but it works!
public class MyObject
{
public string Name { get; set; }
public string Something1{ get; set; }
public string Something2{ get; set; }
public string Something3 { get; set; }
[ScriptIgnore()]
public MyObjectMyObject2 { get; set; }
public string Color { get { return this.MyObject2.Color; } }
public string Something4 { get { return this.MyObject2.Something4; } }
}
public class MyObject2
{
public string Color { get; set; }
public string Something4 { get; set; }
}