Map a flat structure to an object with AutoMapper? - automapper

The data being returned from a stored procedure has 3 columns of repeating data:
Name | Address | PhoneNumber | UniqueCol1 | UniqueCol2
Ideally I want my model to show that there is repeated data by only storing the values once and have a collection of the unique data.
public class MyViewModel
{
public string Name {get;set;}
public string Address {get;set;}
public string PhoneNumber {get;set;}
public List<MyModel> UniqueData {get;set;}
public class MyModel
{
public string UniqueCol1 {get;set;}
public string UniqueCol2 {get;set;}
}
}
This means I want to map a collection of results to a single object of MyViewModel:
return Mapper.Map<List<StoredProcedureObject>, MyViewModel>(result);
This is where I get my error as I assume I have some configuration that I need to do:
Mapping types:
List`1 -> MyViewModel
Any ideas on the missing step to get this to work?

Automapper is only able to flatten your structure into something simpler. But it's not possible to map a simple class to something more specific.
I would suggest to take only the first entry in your table to fill your base fields like Name, Address, PhoneNumber and iterate over your results to fill your UniqueData List.
I don't see an easier way, because with each possible mapping and without using seperate loops you will get your base data multiple times.
If you don't mind to use another tool, maybe you will have a look at ValueInjecter. I heard you can use this tool for two-way-mappings.

Related

Query the hazelcast data having inner object arrays using predicate

I have a user Object, Which has inner Object of type Address. Below are the structures of both User and Address.
User{
String name;
String id;
String phoneNumber;
List<Address> address
}
Address{
String type;
String streetName;
String houseNumber;
String Country;
int pin
}
I am storing the User Objects to the Hazelcast Cache. I would like to query Users whose address type is "Primary" and Country is "US".
The problem I am seeing is that each user can have multiple addresses, How to loop through the address and find the one with type "Primary" and for that particular address type how to query the Country "US". Can we use predicate to achieve this? If so, Please help me with how the predicate can be constructed.
Please check Querying in Collections and Arrays in the Hazelcast documentation.
In your case, you would like to have two conditions on the dependent collection, like:
Predicates.equal("address[any].country", "US")
Predicates.equal("address[any].type", "Primary")
But any should apply to the same entity (because you'd like to have US as Primary country). I don't think you can achieve it with just Predicates.
What you can do, however, is to use Custom Attributes and define your own ValueExtractor which would cover the logic you need.

SSRS 2016 Report - Populate table from string parameter

Let's assume I have the following C# object:
public class BusinessDetails
{
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
}
Long story short my solution [A] sends a serialised (JSON) List<BusinessDetails> to another external solution [B] that in turn feeds the RDL in question with this and other provided parameters (strings); therefore, on the RDL level I am limited to strings and strings only.
This is all good for simple, one value string parameters but I am interested in dynamically populating table from (JSON 'flattened'/string) List<BusinessDetails> in the mentioned RDL.
I see the available parameter types are:
Text
Boolean
Date/Time
Integer
Float
I guess my question is - can I use the Text/string param to dynamically populate a table in RDL?
Thanks.
short anser NO!
long answer: you dont need that, using the report viewer control you can use the list directly as datasource, when using rdlc instead of rdl you can even consume the class in the reportdesigner, you dont have to come up with ways to align your data source mapping with the data you are recieving
read this for further info: Creating a PDF from a RDLC Report in the Background

Automapper: Mapping a property value of an object to a string

Using Automapper, how do you handle the mapping of a property value on an object to an instance of a string. Basically I have a list of Role objects and I want to use Automapper to map the content of each "name" property to a corresponding list of string (so I just end up with a list of strings). I'm sure it has an obvious answer, but I can't find the mapping that I need to add to "CreateMap" to get it to work.
An example of the relevant code is shown below:
public class Role
{
public Guid Id{get;set;}
public string Name{get;set;}
...
...
}
// What goes in here?
Mapper.CreateMap<Role, string>().ForMember(....);
var allRoles = Mapper.Map<IList<Role>, IList<string>>(roles);
I love Automapper (and use it in a number of projects), but wouldn't this be easier with a simple LINQ statement?
var allRoles = from r in roles select r.Name
The AutoMapper way of accomplishing this:
Mapper.CreateMap<Role, String>().ConvertUsing(r => r.Name);

Can Automapper map from a Dictionary of properties to a flat destination?

Source contains a property bag in a Dictionary. Can Automapper map the entries in the Dictionary to individual properties of the Destination based upon matching the dictionary keys with the names of the properties on the destination type?
Example:
public class Destination
{
public int ProdNumber;
public string Title;
}
public class Source
{
public Dictionary<string, object> values = new Dictionary<string, object>();
}
where the values Dictionary will have two entries, one with a key of "ProdNumber" and one with a key value of "Title". There will likely be entries in the dictionary that have keys that don't match any property in the Destination and they should be ignored. There will be multiple properties of each primitive data type (int, string, etc) - so I presume I can't use a simple set of TypeConverters.
Any suggestions?
Thanks,
Chris
Unfortunately it is not possible at the moment, but it is planned for the next version. Read this thread as it discusses the plans and a work around.

SubSonic How to Execute a SQL Statement?

My site is using Subsonic 2.2 on my site.
I have 1 weird situation where I need to run some ad-hoc SQL statements.
public IList<string> GetDistincList(string TableName, string FieldName)
{
string sqlToRun = string.Format("SELECT DISTINCT {0} FROM {1} ORDER BY {0}", FieldName, TableName);
Query query = new Query(TableName);
query.PleaseRunThis(sqlToRun);
query.ExecuteReader();
}
Can anyone help me here? As it appears, I just want to return a generic list of strings.
Thanks!
Subsonic has a great method called ExecuteTypedList() so you can do somethink like this.
List<int> result = DB.Select(Table.Columns.Id)
.Distinct()
.From<Table>()
.OrderBy(Table.Columns.Id)
.ExecuteTypedList<int>();
or even with pocos:
public class UserResult
{
public int Id {get;set;}
public string Name {get;set;}
}
List<UserResult> users = DB.Select(
User.Columns.UserId + " as Id", // the as ... is only needed if your
User.Columns.UserName + " as Name" // column name differs from the
).From<User>() // property name of your class
.ExecuteTypedList<UserResult>();
Unfortunately this method doesn't work for string since it requires
a) a valuetype
b) a class with a parameterless constructor since the method uses reflection to map the columns from the result to the properties of the class
However I wrote an extension method a while ago that works for string:
Use the Subsonic.Select() ExecuteTypedList Method with String
Look at my own answer in the link.
If you add the extensionmethod to your code you can do:
List<String> result = DB.Select(User.Columns.UserName)
.From<User>()
.ExecuteTypedList();
Use the CodingHorror class.
Here's the SubSonic 3 way of doing it: http://www.subsonicproject.com/docs/CodingHorror
The SubSonic 2 way is similar:
Dim ch As SubSonic.CodingHorror
ch.Execute("delete from #tablename", table)

Resources