I have an application that will parse an excel file and add a column, then generate a new CSV file with the results. I am able to create a list of the items I want in the file, but I cannot figure out how to pass that list to the method that is generating the new file.
I have the following class:
public class LocationData
{
public string PostalCode { get; set; }
public string Partner { get; set; }
public string LocationID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Market { get; set; }
}
and the following code to get the data into a list:
LocationData Locationdata = new LocationData()
{
PostalCode = location[0],
Partner = location[1],
LocationID = location[2],
Name = location[3],
Country = location[4],
Market = repository.GetMarketsForPostalCode(location[0])
}
I also have the method to create the csv and I need to pass in the list info, but I get the error:
foreach statement cannot operate on variables of type 'app.LocationData' because 'app.LocationData' does not contain a public definition for 'GetEnumerator'
I think you are misunderstanding what a list is in C#. I think you need the List data type. Try this:
List<string> Locationdata = new List<string>()
{
location[0],
location[1],
location[2],
location[3],
location[4],
repository.GetMarketsForPostalCode(location[0])
};
Your csv function will look like this
public void GenerateCSV(List<LocationData> data)
{
foreach (LocationData d in data)
{
//put line in csv as
string s = d.PostalCode + "," d.Partner + _"," + d.LocationID...... + Environment.NewLine;
}
}
Your class declaration will remain same
public class LocationData
{
public string PostalCode { get; set; }
public string Partner { get; set; }
public string LocationID { get; set; }
public string Name { get; set; }
public string Country { get; set; }
public string Market { get; set; }
}
Now you need to add all the data in the list first
which you will do like this
List<LocationData> lst = new List<LocationData>();
LocationData ld = new LocationData();
ld.LocationID = "0";
ld.Market = "market";
lst.Add(ld);
........
GenerateCSV(lst);
Related
I have 2 classes
public class Product
{
public DateTime Date { get; set; }
public string Name { get; set; }
public int Amount { get; set; }
}
public class Campaign
{
public long CampaignId { get; set; }
public string CampaignName { get; set; }
public List<Product> Products { get; set; }
}
Code:
var campaign = new Campaign();
campaign.CampaignId = Item.CampaignId;
campaign.CampaignId = Item.CampaignId;
campaign.CampaignName = Item.CampaignName;
campaign.Products = productList;
campaignList.Add(campaign);
productList.Clear();
When I call productList.Clear(), my "campaign" deletes its campaign.Products.
How can I prevent that from happening?
campaign.Products = new List<Product>(productList);
because campaign.Products is the same reference of productList
they are both pointing to the same list , any action on one will be reflected in the other varialbe
you need to clone (make another copy of the list) by different ways as follwoing
campaign.Products = productList.GetClone();
or
campaign.Products = productList.ToList();
or
campaign.Products.AddRange(productList);
check the following url
https://www.techiedelight.com/clone-list-in-csharp/
Everywhere I look seems to have the same response but I can't find one to address the issue I am having. I am trying to concatenate the items in a list of objects into a string. However, what I get is the name of the page and the name of the object, the actual list values.
I tried:
string combinedLog = string.Join(",", logList)
I also tried:
string combinedLog = string.Join(",", logList.Select(c => c.ToString()).ToArray<string>());
What I get is PageName + Log, PageName + Log
This is the object:
private class Log
{
public DateTime LogTime { get; set; }
public string Result { get; set; }
public string ItemName { get; set; }
public Guid? ItemId { get; set; }
public string ErrorMessage { get; set; }
}
and the list is:
List<Log> logList = new List<Log>();
I am trying to get a string like:
"10/21/2019, Fail, Acme, Could not Import, 10/21/2019, Success, ABC, no errors"
You can override ToString() method in Log class for that
private class Log
{
public DateTime LogTime { get; set; }
public string Result { get; set; }
public string ItemName { get; set; }
public Guid? ItemId { get; set; }
public string ErrorMessage { get; set; }
public override string ToString()
{
return $"{LogTime}, {Result}, {ItemName}, {ErrorMessage}";
}
}
And than concatenate logList into one string using string.Join
You should do something like
String.Join(";", logList.Select(x => $"{x.LogTime},{x.Result},{x.ItemName}")) ..
Or use generics to get it
var fields = typeof(Log).GetFields();
var result = String.Join(";", logList.Select(x =>
String.Join(",", fields.Select(f => f.GetValue(x)))
));
I'm try to update OFAC Sanction list to my database by using following URL.
URL :
http://www.treasury.gov/ofac/downloads/sdn.csv
This csv file has no any header and can download as a file. So I'm split all the record and get as a string Array. I have a problem with how can I assign those results to my entity object to save.
This is my Code :
List<SanctionOFACEntries> sanctionOFACList = new List<SanctionOFACEntries>();
List<string> splitted = new List<string>();
string fileList = GetCSVFileContent(url);
string[] tempStr;
tempStr = Regex.Matches(fileList, "(?:\"(?<m>[^\"]*)\")|(?<m>[^,]+)").Cast<Match>().Select(m => m.Value).ToArray(); ;
int i = 0;
foreach (string item in tempStr)
{
i += 1;
SanctionOFACEntries sanctionOFAC = new SanctionOFACEntries();
if (i != 1)
{
sanctionOFAC.Name = tempStr[i];
}
}
This case that all the records are assign to array list correctly. If any one have this OFAC sample upload code send me .
Please help this.
Thanks.
I'm not 100% clear on your issue, but first of all, I'd recommend you use a csv library. I'm not clear on what your Regex is attempting to due, but I'm assuming it's trying to parse CSV. Using FileHelpers csv library, I've rewritten your code:
var engine = new FileHelperEngine<SanctionOFACEntries>();
var sanctionOFACList = engine.ReadStringAsList(GetCSVFileContent(url));
Far more straight forward. Note your SanctionOFACEntries class should look something like this Treasury Data Spec:
[DelimitedRecord(",")]
public class SanctionOFACEntries
{
public int ent_num
[FieldQuoted]
public string ent_num { get; set; }
[FieldQuoted]
public string SDN_Name { get; set; }
[FieldQuoted]
public string SDN_Type { get; set; }
[FieldQuoted]
public string Program { get; set; }
[FieldQuoted]
public string Title { get; set; }
[FieldQuoted]
public string Call_Sign { get; set; }
[FieldQuoted]
public string Vess_type { get; set; }
[FieldQuoted]
public string Tonnage { get; set; }
[FieldQuoted]
public string GRT { get; set; }
[FieldQuoted]
public string Vess_flag { get; set; }
[FieldQuoted]
public string Vess_owner { get; set; }
[FieldQuoted]
public string Remarks { get; set; }
}
So first some info about the project; here is a class I created :
public class SendOverview
{
public string id { get; set;}
public string method { get; set;}
public SendOV Params {get; set;}
}
public class SendOV
{
public string overviewID { get; set; }
public string overviewType { get; set; }
public string AORParams { get; set; }
public SentDatas arrOptions { get; set; }
}
public class SentDatas
{
public string columnInfo { get; set; }
public string orderInfo { get; set; }
}
A pretty simple class where I want to serialize the whole thing (So, the SendOverview class) by creating an object as done here :
SendOverview test1 = new SendOverview();
test1.id = "1";
test1.method = "getOverviewInfo";
SendOV testOV = new SendOV();
testOV.AORParams = null;
testOV.overviewID = tempDID;
testOV.overviewType = "Stock Items";
SentDatas col1 = new SentDatas();
col1.columnInfo = "1;100;1;1#";
col1.orderInfo = "1;0;0#";
Now once I try to add the col1 data to testOV's arrOptions I get a nullreference exception which blocks my work from any progress.. I have tried much, to no avail.
testOV.arrOptions[0] = col1;
is giving me the exception; Any help is highly appreciated..
(I know I have to create a List[] xx = new List[MAX] somewhere but I'm not able to implement it.)
COMPLIMENTARY QUESTION :
when sending the json string :
{\"id\":\"1\",\"method\":\"getOverviewInfo\",\"Params\":{\"overviewID\":\"0000004297\",\"overviewType\":\"Stock Items\",\"AORParams\":null,\"arrOptions\":{\"columnInfo\":\"1;100;1;1#\",\"orderInfo\":\"1;0;0#\"}}}"
All the named parameters should only have the value, not the named parameter; adjusted :
{\"id\":\"1\",\"method\":\"getOverviewInfo\",\"Params\":{"0000004297\","Stock Items",null,{\"columnInfo\":\"1;100;1;1#\",\"orderInfo\":\"1;0;0#\"}}}"
Which JSON property should I add to get this effect?
Thank you!
I am not entirely sure I understand what it is you are after, but take a look at the following and see if I am on the right track.
Update your class like so:
public class SendOV
{
public string overviewID { get; set; }
public string overviewType { get; set; }
public string AORParams { get; set; }
public List<SentDatas> arrOptions { get; set; }
}
And then update your creation code to this:
SendOverview test1 = new SendOverview();
test1.id = "1";
test1.method = "getOverviewInfo";
SendOV testOV = new SendOV();
testOV.AORParams = null;
testOV.overviewID = tempDID;
testOV.overviewType = "Stock Items";
List<SentDatas> sentDatasList = new List<SentDatas>();
SentDatas col1 = new SentDatas();
col1.columnInfo = "1;100;1;1#";
col1.orderInfo = "1;0;0#";
sentDatasList.Add(col1);
testOV.arrOptions = sentDatasList;
I'm having some trouble storing and retrieving items into a list<> with a custom structure.
My structure looks like this:
public class list_rss_parameters
{
public string this_string { get; set; }
public string title_start { get; set; }
public string title_end { get; set; }
public string description_start { get; set; }
public string description_end { get; set; }
public string link_start { get; set; }
public string link_end { get; set; }
public string publish_date_start { get; set; }
public string publish_date_end { get; set; }
public string author_start { get; set; }
public string author_end { get; set; }
}
My stored procedure looks like this (and note that the variable names are the same as the custom Key names) Is this ok?
//this is the last part of a custom method that returns a list
List<list_rss_parameters> list_rss_items = new List<list_rss_parameters>();
list_rss_items.Add(new list_rss_parameters()
{
this_string = this_string,
title_start = title_start,
title_end = title_end,
description_start = description_start,
description_end = description_end,
link_start = link_start,
link_end = link_end,
publish_date_start = publish_date_start,
publish_date_end = publish_date_end,
author_start = author_start,
author_end = author_end
});
return list_rss_items;
If the above two setups are correct, how do I pull items out of the List once I return it?
List<list_rss_parameters> list_rss_parameters = new List<list_rss_parameters>();
list_rss_parameters = f_discover_rss_parameters(rss);
show(list_rss_parameters.Count.ToString());
show(list_rss_parameters[0].ToString()); //does not show this_string
show(list_rss_parameters[this_string'] //does not show this_string
show(list_rss_parameters[0][this_string'];//does not show this_string
What am I doing wrong?
You want the this_string property of the first item in your list it seems:
show(list_rss_parameters[0].this_string);
Or show all of them:
foreach(var item in list_rss_parameters)
{
Console.WriteLine(item.this_string);
}
As a side note your property names don't match the PascalCase naming convention for properties in .NET - so this_string really should be ThisString.