csvHelper Sort field while writing - sorting

I am trying to use CsvHelper library to write CSV stream to a file. All is well however I am stuck with 2 things
How do I add additional columns to the csv file. The additional column value will be a constant but will only be known at runtime.
Is there a way to sort the CSV file on a particular field if I am writing one record at a time and also when I am writing all at once
static void Main(string[] args)
{
var records = new List<MyTest>
{
new MyTest { Id = 1, Name = "John", Extra = "hello" },
new MyTest { Id = 2, Name = "Jack", Extra = "hi" },
};
using (var writer = new StreamWriter("C:\\Users\\machina\\AppData\\Local\\Temp\\file.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.Context.RegisterClassMap<TestMap<MyTest>>();
csv.WriteRecords<MyTest>(records);
}
}
}
public interface IData
{
public int Id { get; set; }
public string Name { get; set; }
}
public abstract class AbTest : IData
{
public abstract int Id { get; set; }
public abstract string Name { get; set; }
public abstract string Extra { get; set; }
}
public class MyTest : AbTest
{
public override int Id { get; set; }
public override string Name { get; set; }
public override string Extra { get; set; }
}
public class TestMap<T> : ClassMap<MyTest>
{
public TestMap()
{
AutoMap(CultureInfo.InvariantCulture);
}
}
The above works as I have prepared the data records however what I want to achieve is to dynamically add Extra column values to my existing stream of csv data which does not have it.
Also I saw DynamicPropertySort under csvconfiguration and wanted to check if this can be used in any way to sort the fields in my CSV stream before its written even when its writing one record at a time?
I am a novoice developer so let me know if there is a better way to achieve this.

The easiest way to dynamically add columns would likely be to manually write out each row.
DynamicPropertySort is just used for ordering the columns of a dynamic object. You can sort on a particular field using the linq OrderBy() method.
static void Main(string[] args)
{
var records = new List<MyTest>
{
new MyTest { Id = 1, Name = "John", Extra = "hello" },
new MyTest { Id = 2, Name = "Jack", Extra = "hi" },
};
var extraField1 = 10;
var extraField2 = "Testing";
using (var writer = new StreamWriter("C:\\Users\\machina\\AppData\\Local\\Temp\\file.csv"))
using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csv.Context.RegisterClassMap<TestMap>();
var orderedRecords = records.OrderBy(r => r.Name);
csv.WriteHeader<MyTest>();
csv.WriteField("ExtraField1");
csv.WriteField("ExtraField2");
csv.NextRecord();
foreach (var record in orderedRecords)
{
csv.WriteRecord(record);
csv.WriteField(extraField1);
csv.WriteField(extraField2);
csv.NextRecord();
}
}
}

Related

Passing variables / arguments between repeated Activities in MassTransit Courier Routing Slips

I'm looking to integrate the MassTransit Courier Routing Slip features into an existing solution using Azure Functions and ServiceBusTriggers that synchronizes data between two systems, and has to use a SOAP HTTP client. However, I'm struggling to understand how arguments and variables passed between activities are prioritized. This is best explained via a poor mock example itinerary. My assumption was that variables override existing arguments, but I think that was an incorrect assumption.
public class SyncCustomerOrderConsumer : IConsumer<SyncCustomerOrderMessage>
{
public async Task Consume(ConsumeContext<SyncCustomerOrderMessage> context)
{
var slip = this.BuildRoutingSlip(context.Message);
await context.Execute(slip);
}
private RoutingSlip BuildRoutingSlip(SyncCustomerOrderMessage args)
{
var builder = new RoutingSlipBuilder(NewId.NextGuid());
builder.AddVariable("OrderItems", args.OrderItems);
builder.AddActivity(nameof(SyncCustomerActivity), GetActivityCustomer<SyncCustomerActivity, SyncCustomerArgs>() new {
args.Customer
});
builder.AddActivity(nameof(SyncCustomerActivity), GetActivityCustomer<SyncCustomerActivity, SyncCustomerArgs>() new {
args.ShippingAddress
});
builder.AddActivity(nameof(SyncOrderActivity), GetActivityCustomer<SyncOrderActivity, SyncOrderArgs>() new {
args.Order
});
foreach (var item in args.OrderItems)
{
builder.AddActivity(nameof(SyncOrderItemActivity), GetActivityCustomer<SyncOrderItemActivity, SyncOrderItemArgs>(), new
{
OrderItem = args.item
});
}
builder.AddActivity(nameof(SyncSourceActivity), GetActivityCustomer<SyncSourceActivity, SyncSourceArgs>());
return builder.Build();
}
}
public class SyncOrderItemActivity : IExecuteActivity<SyncOrderItemArgs>
{
private readonly IOrderItemWebserviceClient _client;
privater readonly IMapper _mapper;
public SyncOrderItemActivity(IOrderItemWebserviceClient client, IMapper mapper)
{
_client = client;
_mapper = mapper;
}
public async Task<ExecutionResult> Execute(ExecuteContext<SyncOrderItemArgs> context)
{
var args = context.Arguments;
var dto = _mapper.Map<OrderItemDto>(args);
if (args.OrderItem.External.IsNotSynced())
{
var response = await _client.AddAsync(dto);
args.OrderItem.ExternalId = response.Uuid;
args.OrderItem.LastSynced = response.LastUpdated;
}
else
{
var response = await _client.UpdateAsync(dto);
args.OrderItem.LastSynced = response.LastUpdated;
}
// replace the existing order items variable
int index = args.OrderItems.FindIndex(oi => oi.Id == args.OrderItem.Id);
if (index != 1)
args.OrderItems[index] = orderItem;
return context.CompletedWithVariables(new { OrderItem = args.OrderItem, OrderItems = args.OrderItems });
}
}
public class SyncOrderItemArgs
{
public OrderItem OrderItem { get; set; }
public List<OrderItem> OrderItems { get; set; }
}
public class SyncSourceActivity : IExecuteActivity<SyncSourceArgs>
{
private readonly IEventGridClient _client;
privater readonly IMapper _mapper;
public SyncSourceActivity(IEventGridClient client, IMapper mapper)
{
_client = client;
_mapper = mapper;
}
public async Task<ExecutionResult> Execute(ExecuteContext<SyncSourceArgs> context)
{
var args = context.Arguments;
// this is the original list, not the replaced list
foreach (var item in args.OrderItems)
{
await _client.PublishAsync(new OrderItemSyncedEvent { item });
}
return context.Completed();
}
}
public class SyncCustomerOrderMessage
{
public Customer Customer { get; set; }
public Order Order { get; set; }
public List<OrderItem> OrderItems { get; set; }
public Address ShippingAddress { get; set; }
}
The problem here is that the list of activities to deal with each OrderItem is defined as an argument and updated in each call to that SynOrderItemactivity. As the individual item is processed, it is supposed to replace the original item in the list and then pass the entire altered list as a variable into the next iteration of the same activity. However, the list is not the altered list, but the original one.
I guess my question is two-fold:
How do should you best design a routing slip that has a a list if the same activity, where some of the arguments have to be defined, but others are expected to come from the variable?
When it comes to arguments and variables, which ones take priority?
Unknown? I'm not even sure what this question is asking.
Arguments first, variables second. Explicitly specified arguments when adding the activity to the itinerary take precedence, missing arguments are pulled from variables if present, or left at the default/null value.

How to send complex objects in GET to WEB API 2

Let's say that you have the following code
public class MyClass {
public double Latitude {get; set;}
public double Longitude {get; set;}
}
public class Criteria
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public MyClass MyProp {get; set;}
}
[HttpGet]
public Criteria Get([FromUri] Criteria c)
{
return c;
}
I'd like to know if someone is aware of a library that could transform any object into query string that is understood by a WEB API 2 Controller.
Here is an example of what I'd like
SerializeToQueryString(new Criteria{StartDate=DateTime.Today, EndDate = DateTime.Today.AddDays(1), MyProp = new MyProp{Latitude=1, Longitude=3}});
=> "startDate=2015-10-13&endDate=2015-10-14&myProp.latitude=1&myProp.longitude=3"
A full example with httpClient might look like :
new HttpClient("http://localhost").GetAsync("/tmp?"+SerializeToQueryString(new Criteria{StartDate=DateTime.Today, EndDate = DateTime.Today.AddDays(1), MyProp = new MyProp{Latitude=1, Longitude=3}})).Result;
At the moment, I use a version (taken from a question I do not find again, maybe How do I serialize an object into query-string format? ...).
The problem is that it is not working for anything else than simple properties.
For example, calling ToString on a Date will not give something that is parseable by WEB API 2 controller...
private string SerializeToQueryString<T>(T aObject)
{
var query = HttpUtility.ParseQueryString(string.Empty);
var fields = typeof(T).GetProperties();
foreach (var field in fields)
{
string key = field.Name;
var value = field.GetValue(aObject);
if (value != null)
query[key] = value.ToString();
}
return query.ToString();
}
"Transform any object to a query string" seems to imply there's a standard format for this, and there just isn't. So you would need to pick one or roll your own. JSON seems like the obvious choice due to the availability of great libraries.
Since it seems no one has dealt with the problem before, here is the solution I use in my project :
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Web;
namespace App
{
public class QueryStringSerializer
{
public static string SerializeToQueryString(object aObject)
{
return SerializeToQueryString(aObject, "").ToString();
}
private static NameValueCollection SerializeToQueryString(object aObject, string prefix)
{
//!\ doing this to get back a HttpValueCollection which is an internal class
//we want a HttpValueCollection because toString on this class is what we want in the public method
//cf http://stackoverflow.com/a/17096289/1545567
var query = HttpUtility.ParseQueryString(String.Empty);
var fields = aObject.GetType().GetProperties();
foreach (var field in fields)
{
string key = string.IsNullOrEmpty(prefix) ? field.Name : prefix + "." + field.Name;
var value = field.GetValue(aObject);
if (value != null)
{
var propertyType = GetUnderlyingPropertyType(field.PropertyType);
if (IsSupportedType(propertyType))
{
query.Add(key, ToString(value));
}
else if (value is IEnumerable)
{
var enumerableValue = (IEnumerable) value;
foreach (var enumerableValueElement in enumerableValue)
{
if (IsSupportedType(GetUnderlyingPropertyType(enumerableValueElement.GetType())))
{
query.Add(key, ToString(enumerableValueElement));
}
else
{
//it seems that WEB API 2 Controllers are unable to deserialize collections of complex objects...
throw new Exception("can not use IEnumerable<T> where T is a class because it is not understood server side");
}
}
}
else
{
var subquery = SerializeToQueryString(value, key);
query.Add(subquery);
}
}
}
return query;
}
private static Type GetUnderlyingPropertyType(Type propType)
{
var nullablePropertyType = Nullable.GetUnderlyingType(propType);
return nullablePropertyType ?? propType;
}
private static bool IsSupportedType(Type propertyType)
{
return SUPPORTED_TYPES.Contains(propertyType) || propertyType.IsEnum;
}
private static readonly Type[] SUPPORTED_TYPES = new[]
{
typeof(DateTime),
typeof(string),
typeof(int),
typeof(long),
typeof(float),
typeof(double)
};
private static string ToString(object value)
{
if (value is DateTime)
{
var dateValue = (DateTime) value;
if (dateValue.Hour == 0 && dateValue.Minute == 0 && dateValue.Second == 0)
{
return dateValue.ToString("yyyy-MM-dd");
}
else
{
return dateValue.ToString("yyyy-MM-dd HH:mm:ss");
}
}
else if (value is float)
{
return ((float) value).ToString(CultureInfo.InvariantCulture);
}
else if (value is double)
{
return ((double)value).ToString(CultureInfo.InvariantCulture);
}
else /*int, long, string, ENUM*/
{
return value.ToString();
}
}
}
}
Here is the unit test to demonstrate :
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Framework.WebApi.Core.Tests
{
[TestClass]
public class QueryStringSerializerTest
{
public class EasyObject
{
public string MyString { get; set; }
public int? MyInt { get; set; }
public long? MyLong { get; set; }
public float? MyFloat { get; set; }
public double? MyDouble { get; set; }
}
[TestMethod]
public void TestEasyObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject(){MyString = "string", MyInt = 1, MyLong = 1L, MyFloat = 1.5F, MyDouble = 1.4});
Assert.IsTrue(queryString.Contains("MyString=string"));
Assert.IsTrue(queryString.Contains("MyInt=1"));
Assert.IsTrue(queryString.Contains("MyLong=1"));
Assert.IsTrue(queryString.Contains("MyFloat=1.5"));
Assert.IsTrue(queryString.Contains("MyDouble=1.4"));
}
[TestMethod]
public void TestEasyObjectNullable()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject() { });
Assert.IsTrue(queryString == "");
}
[TestMethod]
public void TestUrlEncoding()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EasyObject() { MyString = "&=/;+" });
Assert.IsTrue(queryString.Contains("MyString=%26%3d%2f%3b%2b"));
}
public class DateObject
{
public DateTime MyDate { get; set; }
}
[TestMethod]
public void TestDate()
{
var d = DateTime.ParseExact("2010-10-13", "yyyy-MM-dd", CultureInfo.InvariantCulture);
var queryString = QueryStringSerializer.SerializeToQueryString(new DateObject() { MyDate = d });
Assert.IsTrue(queryString.Contains("MyDate=2010-10-13"));
}
[TestMethod]
public void TestDateTime()
{
var d = DateTime.ParseExact("2010-10-13 20:00", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture);
var queryString = QueryStringSerializer.SerializeToQueryString(new DateObject() { MyDate = d });
Assert.IsTrue(queryString.Contains("MyDate=2010-10-13+20%3a00%3a00"));
}
public class InnerComplexObject
{
public double Lat { get; set; }
public double Lon { get; set; }
}
public class ComplexObject
{
public InnerComplexObject Inner { get; set; }
}
[TestMethod]
public void TestComplexObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new ComplexObject() { Inner = new InnerComplexObject() {Lat = 50, Lon = 2} });
Assert.IsTrue(queryString.Contains("Inner.Lat=50"));
Assert.IsTrue(queryString.Contains("Inner.Lon=2"));
}
public class EnumerableObject
{
public IEnumerable<int> InnerInts { get; set; }
}
[TestMethod]
public void TestEnumerableObject()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EnumerableObject() {
InnerInts = new[] { 1,2 }
});
Assert.IsTrue(queryString.Contains("InnerInts=1"));
Assert.IsTrue(queryString.Contains("InnerInts=2"));
}
public class ComplexEnumerableObject
{
public IEnumerable<InnerComplexObject> Inners { get; set; }
}
[TestMethod]
public void TestComplexEnumerableObject()
{
try
{
QueryStringSerializer.SerializeToQueryString(new ComplexEnumerableObject()
{
Inners = new[]
{
new InnerComplexObject() {Lat = 50, Lon = 2},
new InnerComplexObject() {Lat = 51, Lon = 3},
}
});
Assert.Fail("we should refuse something that will not be understand by the server");
}
catch (Exception e)
{
Assert.AreEqual("can not use IEnumerable<T> where T is a class because it is not understood server side", e.Message);
}
}
public enum TheEnum : int
{
One = 1,
Two = 2
}
public class EnumObject
{
public TheEnum? MyEnum { get; set; }
}
[TestMethod]
public void TestEnum()
{
var queryString = QueryStringSerializer.SerializeToQueryString(new EnumObject() { MyEnum = TheEnum.Two});
Assert.IsTrue(queryString.Contains("MyEnum=Two"));
}
}
}
I'd like to thank all the participants even if this is not something that you should usually do in a Q&A format :)

How to store multiple data in isolated storage setting in wp7?

I want to store multiple name in Isolated storage.And i use this code for store multopple storage
if (!setting.Contains("CityDetail"))
{
setting.Add("CityDetail", cityname);
}
else
{
setting["CityDetail"] = cityname;
}
setting.Save();
But it gives only last added value,h=So how can i fetch all the data base value?
You can use these function to store your objects in phoneapplication state
public static void SaveState(string key, object value)
{
if (phoneApplicationPage.State.ContainsKey(key))
{
phoneApplicationPage.State.Remove(key);
}
phoneApplicationPage.State.Add(key, value);
}
public static object LoadState(this PhoneApplicationPage phoneApplicationPage, string key)
{
if (phoneApplicationPage.State.ContainsKey(key))
{
return phoneApplicationPage.State[key];
}
return null;
}
Edit-Using isolatedsettings
public void SaveCompositeObject()
{
var settings = IsolatedStorageSettings.ApplicationSettings;
Person person= new City { Name = "Alex", Age= "21" };
settings.Add("person", person);
}
public class Person
{
public string Name
{
get;
set;
}
public string Age
{
get;
set;
}
}
retrieving data
Person person1;
settings.TryGetValue<Person>("person", out person11);

Trying to string.Join an IList and outputting the results to console

Using "string.Join(",", test);" works but for some reason I get an output of:
"Ilistprac.Location, Ilistprac.Location, Ilistprac.Location"
I tried ToString, Convert.ToString, etc and I still get that output.
All the IList interfaces are implemented with the IEnurmerable too (just not listed here unless someone wants me to).
class IList2
{
static void Main(string[] args)
{
string sSite = "test";
string sBldg = "test32";
string sSite1 = "test";
string sSite2 = "test";
Locations test = new Locations();
Location loc = new Location();
test.Add(sSite, sBldg)
test.Add(sSite1)
test.Add(sSite2)
string printitout = string.Join(",", test); //having issues outputting whats on the list
}
}
string printitout = string.Join(",", test.ToArray<Location>);
public class Location
{
public Location()
{
}
private string _site = string.Empty;
public string Site
{
get { return _site; }
set { _site = value; }
}
}
public class Locations : IList<Location>
{
List<Location> _locs = new List<Location>();
public Locations() { }
public void Add(string sSite)
{
Location loc = new Location();
loc.Site = sSite;
loc.Bldg = sBldg;
_locs.Add(loc);
}
private string _bldg = string.Empty;
public string Bldg
{
get { return _bldg; }
set { _bldg = value; }
}
}
You need to supply a useful ToString implementation for Location as Join is calling that for each element. The default implementation will just return the name of the type. See documentation.
So if you have a type like
class SomeType
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public SomeType(string first, string last)
{
FirstName = first;
LastName = last;
}
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
You need to specify how that should be represented as a string. If you do that, you can use string.Join like this to produce the output below.
var names = new List<SomeType> {
new SomeType("Homer", "Simpson"),
new SomeType("Marge", "Simpson")
};
Console.WriteLine(string.Join("\n", names));
Output:
Simpson, Homer
Simpson, Marge
You have to override ToString() inc your Location class to provide some meaningful output if you want to keep your current approach, E.g.:
public override string ToString()
{
return Site;
}

Is it possible to return IEnumerable of anonymous objects from DataContext.ExecuteQuery?

I develop a reporting engine where reports are based on templates. Every template has string with SQL query and every report has specific values for SQL query parameters. To render a report I set parameters and call DataContext.ExecuteQuery method to get list of records. But to catch returned columns I have to know their names and have a class with corresponding properties.
Is it possible somehow to return IEnumerable of anonymous objects from DataContext.ExecuteQuery and then determine their properties using Reflection?
I need a LINQ equivalent for SqlDataReader.GetValues.
Thanks!
Until we have C# 4.0 with dynamiс keyword we can use this solution (slightly modified code from an article Executing arbitrary queries in LINQ to SQL by Octavio Hernández Leal):
public static class DataContextExtension
{
public static IEnumerable<Dictionary<string, object>> ExecuteQuery(this DataContext dataContext, string query)
{
using (DbCommand command = dataContext.Connection.CreateCommand())
{
command.CommandText = query;
dataContext.Connection.Open();
using (DbDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
{
while (reader.Read())
{
Dictionary<string, object> dictionary = new Dictionary<string, object>();
for (int i = 0; i < reader.FieldCount; i++)
dictionary.Add(reader.GetName(i), reader.GetValue(i));
yield return dictionary;
}
}
}
}
}
This extension method returns IEnumerable of Dictionary<> objects where keys are names of query columns.
Yes, you can do it.
Please have a look at this snippet.
class Program {
static void Main(string[] args) {
var persons = new Person[]{
new Person{Age=22,Name="John Doe",Id=1},
new Person{Age=23,Name="Jack Smith", Id=2},
new Person{Age=34,Name="Sara Parker", Id=3}
};
var anonData = GetAnonTypes(persons);
foreach (var item in anonData as IEnumerable) {
//use reflection to access propties
}
}
static object GetAnonTypes(IEnumerable<Person> persons) {
var query=from p in persons select new{
Id=p.Id,
Name=p.Name
};
return query;
}
}
public class Person {
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}

Resources