I'm using the CsvHelper library tool to help write lists that I've created to CSV file.
using (var sr = new StreamReader(inPath))
{
using (var sw = new StreamWriter(outPath))
{
var reader = new CsvReader(sr);
var writer = new CsvWriter(sw);
IEnumerable records = reader.GetRecords<DataRecord>().ToList();
List<CountAndFrequencyClass> list1 = new List<CountAndFrequencyClass>();
list1 = CountAndFrequency(records, "ShipperName", 1);
List<CountAndFrequencyClass> list2 = new List<CountAndFrequencyClass>();
list2 = CountAndFrequency(records, "ShipperCity", 1);
list1 = list1.Concat(list2).ToList();
writer.WriteRecords(list2);
}
}
The list1=list1.Concat(list2).ToList(); does indeed concatenate the strings, but it stacks them on top of each other when they're written out to the CSV file. I want to find a way to concatenate the lists horizontally (so they're displayed next to eachother) instead of vertically.
Thanks for any help and please let me know if additional information is needed!
You can use a loop to add list2 column to records of list1
foreach (var i=0 ; i< list1.Count; i++)
{
if(i>=list2.Count ) break;
var rec1 = list1[i];
var rec2 = list2[i];
rec1.NewColumn = rec2.ColumnToAdd;
}
You could write them to the file as you loop through the lists.
using (var sr = new StreamReader(inPath))
{
using (var sw = new StreamWriter(outPath))
{
var reader = new CsvReader(sr);
var writer = new CsvWriter(sw);
IEnumerable records = reader.GetRecords<DataRecord>().ToList();
List<CountAndFrequencyClass> list1 = new List<CountAndFrequencyClass>();
list1 = CountAndFrequency(records, "ShipperName", 1);
List<CountAndFrequencyClass> list2 = new List<CountAndFrequencyClass>();
list2 = CountAndFrequency(records, "ShipperCity", 1);
for (int i = 0; i < list1.Count; i++)
{
writer.WriteRecord(list1[i]);
writer.WriteRecord(list2[i]);
writer.NextRecord();
}
}
}
Related
I have 5 datasets coming from Database and have saved them in List like below:
var list1 = new List<Class1>
var list2 = new List<Class2>
var list3 = new List<Class3>
var list4 = new List<Class4>
var list5 = new List<Class5>
I need to convert all 5 lists into a csv file (excel type).
I can do this only for lis1 dataset as of now.
How can we merge all list and print the data in CSV?
The format of the csv file is as follows
Year,Make,Model,Length
1997,Ford,E350,2.35
2000,Mercury,Cougar,2.38
The following code shows how to implement it or you can use ready-made libraries.
public void WriteToCSV()
{
var csv = new StringBuilder();
foreach (var item in list1)
{
string line = "field1,field2,...";
csv.AppendLine(line);
line = string.Format("{0},{1},...",item.field1,...);
csv.AppendLine(line);
}
//.........................................
//.........................................
foreach (var item in list5)
{
string line = "field1,field2,...";
csv.AppendLine(line);
line = string.Format("{0},{1},...",item.field1,...);
csv.AppendLine(line);
}
string fileName = #"D:\WriteText.csv";
if (File.Exists(fileName))
System.IO.File.AppendAllText(fileName, csv.ToString());
else
System.IO.File.WriteAllText(fileName, csv.ToString());
}
I created a Yaml that looks like this:
Directories:
- ./Libraries:
- DLLList.yml
- ./Output:
- None
Now I deserialized that yaml into a list of Objects:
List<object> allDirectoriesList = new List<object>();
List<string> allFileNames = new List<string>();
using (var reader = new StringReader(File.ReadAllText("./FileConfig.yml")))
{
allDirectoriesList = deserializer.Deserialize<dynamic>(reader)["Directories"] as List<Object>;
}
foreach (var directory in allDirectoriesList)
{
var directoryAsDictionary = (Dictionary<object, object>)directory;
List<object> list = directoryAsDictionary.Select(kvp => kvp.Value).ToList();
IEnumerable<string> _fileList = list.Select(i => i.ToString());
List<string> fileList = _fileList.ToList<string>();
for (int i = 0; i < fileList.Count(); i++)
{
var x = (string)list[i];
}
}
directory is an object of type Dictionary where I converted it into a List in this part:
var directoryAsDictionary = (Dictionary<object, object>)directory;
List<object> list = directoryAsDictionary.Select(kvp => kvp.Value).ToList();
This list contains 1 object of type string, where the filename is stored. But I can't get these strings out of the objects. If I cast them, or convert them ToString(), I always get "System.Collections.Generic.List`1[System.Object]", but it has to be "DLLList.yml" in this case
Assuming you are using YamlDotNet:
List<object> allDirectoriesList = new List<object>();
using (var reader = new StringReader(File.ReadAllText("./FileConfig.yml")))
{
allDirectoriesList = new DeserializerBuilder().Build().Deserialize<dynamic>(reader)["Directories"] as List<object>;
}
foreach (var directory in allDirectoriesList)
{
var directoryAsDictionary = (Dictionary<object, object>)directory;
List<object> list = directoryAsDictionary.SelectMany(kvp => (List<object>)kvp.Value).ToList();
List<string> _fileList = list.Select(Convert.ToString).ToList();
foreach(var file in _fileList)
Console.WriteLine($"Item: {file} found in {Convert.ToString(directoryAsDictionary.Keys.First())}");
}
Basically you were trying to turn the dictionary value to a string, but it was a List. By using SelectMany, it can flatten all the lists into one and use that. There were a few redundant casts, which I've also removed. For future reference, try to make your structures as simple as possible and deserialise them into structs/classes - you'll find this a lot easier that way.
I'm a beginner for coding ,and I was trying to create a search engine , but there s a part I dont know how to solve it that returns an IList as a generic,
public IList<T> Search<T>(string textSearch)
{
IList<T> list = new List<T>();
var result = new DataTable();
using (Analyzer analyzer = new PanGuAnalyzer())
{
var queryParser = new QueryParser(Version.LUCENE_30, "FullText", analyzer);
queryParser.AllowLeadingWildcard = true;
var query = queryParser.Parse(textSearch);
var collector = TopScoreDocCollector.Create(1000, true);
Searcher.Search(query, collector);
var matches = collector.TopDocs().ScoreDocs;
result.Columns.Add("Title");
result.Columns.Add("Starring");
result.Columns.Add("ID");
foreach (var item in matches)
{
var id = item.Doc;
var doc = Searcher.Doc(id);
var row = result.NewRow();
row["Title"] = doc.GetField("Title").StringValue;
row["Starring"] = doc.GetField("Starring").StringValue;
row["ID"] = doc.GetField("ID").StringValue;
result.Rows.Add(row);
}
}
return result;
}
but in this code , I couldn't return result ,it says Cannot Implicitly convert type 'Data.DataTable' to 'Generic.IList',An explicit conversion exists.so how can I solve this?
I guess you don't want to support generics since it doesn't make sense and is impossible. You have a class, for example Film, then return a List<Film>, you don't need the DataTable:
public IList<Film> SearchFilms(string textSearch)
{
IList<Film> list = new List<Film>();
using (Analyzer analyzer = new PanGuAnalyzer())
{
var queryParser = new QueryParser(Version.LUCENE_30, "FullText", analyzer);
queryParser.AllowLeadingWildcard = true;
var query = queryParser.Parse(textSearch);
var collector = TopScoreDocCollector.Create(1000, true);
Searcher.Search(query, collector);
var matches = collector.TopDocs().ScoreDocs;
foreach (var item in matches)
{
var film = new Film();
var id = item.Doc;
var doc = Searcher.Doc(id);
film.Title = doc.GetField("Title").StringValue;
film.Starring = doc.GetField("Starring").StringValue;
film.ID = doc.GetField("ID").StringValue;
list.Add(film);
}
}
return list;
}
Your return statement should be
result.AsEnumerable().ToList();
Don't forget to add namespace
using System.Linq;
I have a wcf ksoap2 service that returns Dictionary<ArrayList, List<byte[]>>. Now at android side I want to fill my Dictionary<String[], ArrayList<Object>> diction; from wcf response. I am new to wcf and android/java, I don't have idea how to do this. Please provide me some better example of filling Dictionary with wcf.
Thanks in advance
This is my wcf code
public Dictionary<ArrayList, List<byte[]>> getImages()
{
Dictionary<ArrayList, List<byte[]>> image_Name = new Dictionary<ArrayList, List<byte[]>>();
DirectoryInfo directoryInfo = new DirectoryInfo(#"C:\Users\Yakhtar\Desktop\abc");
arr1 = new ArrayList();
foreach (FileInfo fi in directoryInfo.GetFiles())
arr1.Add(fi.FullName);
list = new List<byte[]>();
for (int i = 0; i < arr1.Count; i++)
{
img = Image.FromFile(arr1[i].ToString());
ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
list.Add(ms.ToArray());
}
image_Name.Add(arr1, list);
//image_Name[arr1 as ArrayList] = [list as byte[]];
return image_Name;
}
Well I am not sure about that but have you thought about JSON parsing instead of ksoap2 ??
Here is a tutorial on how to work with array of complex objects with KSOAP. I found out by countless hours of debugging. Hope this hepls
also try this
SoapObject countryDetails = (SoapObject)envelope.getResponse();
System.out.println(countryDetails.toString());
ArrayList list = new ArrayList(countryDetails.getPropertyCount());
lv_arr = new String[countryDetails.getPropertyCount()];
for (int i = 0; i < countryDetails.getPropertyCount(); i++) {
Object property = countryDetails.getProperty(i);
if (property instanceof SoapObject) {
SoapObject countryObj = (SoapObject) property;
String countryName = countryObj.getProperty("countryName").toString();
list.add(countryName );
}
}
Do something like this..
list = new List<byte[]>();
for (int i = 0; i < arr1.Count; i++)
{
img = Image.FromFile(arr1[i].ToString());
ms = new MemoryStream();
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
list.Add(ms.ToArray());
}
image_Name.Add(arr1, list);
//image_Name[arr1 as ArrayList] = [list as byte[]];
return image_Name;
}
I tried already some Solutions from here.. without success!
This is my code and the error message below,
SQLite.SQLiteConnection connection = new SQLite.SQLiteConnection(dbPath);
using (var db = new SQLite.SQLiteConnection(dbPath))
{
int i = 0;
var d = from x in db.Table<stations>() select x;
foreach (var sd in d)
{
pushpin[] Tanke = new pushpin[i];
Tanke[i].Titel = sd.name.ToString(); //IndexOutOfRangeException (see below)
Tanke[i].Text = sd.brand.ToString();
Tanke[i].longitude = sd.longitude;
Tanke[i].latitude = sd.latitude;
MapLayer.SetPosition(Tanke[i], new Location(Tanke[i].latitude, Tanke[i].longitude));
pinLayer.Children.Add(Tanke[i]);
ToolTipService.SetToolTip(Tanke[i], Tanke[i].Titel);
i++;
}
db.Dispose();
db.Close();
}
You are creating an array of zero elements when i is zero.
pushpin[] Tanke = new pushpin[i];
Tanke[i].Titel = sd.name.ToString();
Then you access the first element using [0]. That won't work. There is no element in a zero element array.
The problem is how you are creating your array of pushpin. It doesn't look like you need the array so do this instead:
SQLite.SQLiteConnection connection = new SQLite.SQLiteConnection(dbPath);
using (var db = new SQLite.SQLiteConnection(dbPath))
{
var d = from x in db.Table<stations>() select x;
foreach (var sd in d)
{
var tmp = new pushpin();
tmp.Titel = sd.name.ToString(); //IndexOutOfRangeException (see below)
tmp.Text = sd.brand.ToString();
tmp.longitude = sd.longitude;
tmp.latitude = sd.latitude;
MapLayer.SetPosition(tmp, new Location(tmp.latitude, tmp.longitude));
pinLayer.Children.Add(tmp);
ToolTipService.SetToolTip(tmp, tmp.Titel);
}
db.Dispose();
db.Close();
}
While you are stepping over entries in a foreach and create each time a Array of zero elements ( i = 0 ) you can't save something becauste the [0] element is not existing