Adding a new line in "string.join()" when formatting arrays? - c#

Hello I am a newbie programmer, I am trying to format arrays in a way where there is a line break at the end of a specific set of arrays. I currently have 4 separate arrays in which I want to arrange each item in the array to a specific pattern. I have accomplished this task but now I am stumped because they are all in one line. Let me give you an example: (I am doing this on a datagridview by the way)
(This is what I want to happen)
Binder Clips Small pc 12 1260
Selleys All Clear pc 12 2400
(This is what I am getting)
Binder Clips Small pc 12 1260 Selleys All Clear pc 12 2400
This is my code:
//I get these from a datagridview
var items = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[1].Value.ToString().Trim())
.ToArray();
var units = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[2].Value.ToString().Trim())
.ToArray();
var quantity = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[6].Value.ToString().Trim())
.ToArray();
var prices = carto.Rows
.Cast<DataGridViewRow>()
.Select(x => x.Cells[8].Value.ToString().Trim())
.ToArray();
//this is what I use to sort out the pattern that I want the arrays to be in
string[] concat = new string[items.Length * 4];
int index = 0;
for (int i = 0; i < items.Length; i++)
{
concat[index++] = items[i];
concat[index++] = units[i];
concat[index++] = quantity[i];
concat[index++] = prices[i];
}
// and this is where I am stuck because I can just put \n, it would ruin the format even more
cartitems.Text = string.Join(" ", concat);
I also tried doing something like this:
int j = 0;
string str = "";
foreach (var item in concat)
{
str += item;
if (j <= concat.Length - 1)
{
if (j % 3 == 0)
str += " ";
else
str += "\n";
}
j++;
}
It kinda gets the job done but the line breaks are all over the place.
This is what my projects look like so you can get a better gist on where am I getting the data from the 4 arrays:
basically the product name, unit, quantity and line total
and lastly I am storing in on a label so I can see how it the formatting looks like:
that about sums up my problem, I really hope you can help a newbie like me out, I have a feeling the answer is quite simple and I am just un-experienced.

As a general rule, you should keep your data structures (how the data is stored, implemented here as an array of values) separate from the data representation (in this case, written to a list box).
C# is an object-oriented language, so we might as well take advantage of that, right?
Create a class for your items.
class Item
{
public string Name { get; set; }
public string Unit { get; set; }
public string Quantity { get; set; }
public string Price { get; set; }
override public string ToString() {
return $"{Name} {Unit} {Quantity} {Price}";
}
}
This is how you load your array.
Item[] concat = new Item[items.Length];
int index = 0;
for (int i = 0; i < items.Length; i++) {
concat[index++] = new Item {
Name = items[i],
Unit = units[i],
Quantity = quantity[i],
Price = prices[i]
};
}
and this is how you can add the list of items to a listbox.
foreach(Item item in concat) {
listBox.Items.Add(item);
}

Related

How can I split a string to store contents in two different arrays in c#?

The string I want to split is an array of strings.
the array contains strings like:
G1,Active
G2,Inactive
G3,Inactive
.
.
G24,Active
Now I want to store the G's in an array, and Active or Inactive in a different array. So far I have tried this which has successfully store all the G's part but I have lost the other part. I used Split fucntion but did not work so I have tried this.
int i = 0;
for(i = 0; i <= grids.Length; i++)
{
string temp = grids[i];
temp = temp.Replace(",", " ");
if (temp.Contains(' '))
{
int index = temp.IndexOf(' ');
grids[i] = temp.Substring(0, index);
}
//System.Console.WriteLine(temp);
}
Please help me how to achieve this goal. I am new to C#.
If I understand the problem correctly - we have an array of strings Eg:
arrayOfStrings[24] =
{
"G1,Active",
"G2,Inactive",
"G3,Active",
...
"G24,Active"
}
Now we want to split each item and store the g part in one array and the status into another.
Working with arrays the solution is to - traverse the arrayOfStrings.
Per each item in the arrayOfStrings we split it by ',' separator.
The Split operation will return another array of two elements the g part and the status - which will be stored respectively into distinct arrays (gArray and statusArray) for later retrieval. Those arrays will have a 1-to-1 relation.
Here is my implementation:
static string[] LoadArray()
{
return new string[]
{
"G1,Active",
"G2,Inactive",
"G3,Active",
"G4,Active",
"G5,Active",
"G6,Inactive",
"G7,Active",
"G8,Active",
"G9,Active",
"G10,Active",
"G11,Inactive",
"G12,Active",
"G13,Active",
"G14,Inactive",
"G15,Active",
"G16,Inactive",
"G17,Active",
"G18,Active",
"G19,Inactive",
"G20,Active",
"G21,Inactive",
"G22,Active",
"G23,Inactive",
"G24,Active"
};
}
static void Main(string[] args)
{
string[] myarrayOfStrings = LoadArray();
string[] gArray = new string[24];
string[] statusArray = new string[24];
int index = 0;
foreach (var item in myarrayOfStrings)
{
var arraySplit = item.Split(',');
gArray[index] = arraySplit[0];
statusArray[index] = arraySplit[1];
index++;
}
for (int i = 0; i < gArray.Length; i++)
{
Console.WriteLine("{0} has status : {1}", gArray[i] , statusArray[i]);
}
Console.ReadLine();
}
seems like you have a list of Gxx,Active my recomendation is first of all you split the string based on the space, which will give you the array previoulsy mentioned doing the next:
string text = "G1,Active G2,Inactive G3,Inactive G24,Active";
string[] splitedGItems = text.Split(" ");
So, now you have an array, and I strongly recommend you to use an object/Tuple/Dictionary depends of what suits you more in the entire scenario. for now i will use Dictionary as it seems to be key-value
Dictionary<string, string> GxListActiveInactive = new Dictionary<string, string>();
foreach(var singleGItems in splitedGItems)
{
string[] definition = singleGItems.Split(",");
GxListActiveInactive.Add(definition[0], definition[1]);
}
What im achiving in this code is create a collection which is key-value, now you have to search the G24 manually doing the next
string G24Value = GxListActiveInactive.FirstOrDefault(a => a.Key == "G24").Value;
just do it :
var splitedArray = YourStringArray.ToDictionary(x=>x.Split(',')[0],x=>x.Split(',')[1]);
var gArray = splitedArray.Keys;
var activeInactiveArray = splitedArray.Values;
I hope it will be useful
You can divide the string using Split; the first part should be the G's, while the second part will be "Active" or "Inactive".
int i;
string[] temp, activity = new string[grids.Length];
for(i = 0; i <= grids.Length; i++)
{
temp = grids[i].Split(',');
grids[i] = temp[0];
activity[i] = temp[1];
}

Total characters in List c#

I want to count all the characters in my list to see if I surpass the maxvalue of MaxJsonLength.
This is my controller where I have a list with products.
public JsonResult GetAllProducts()
{
List<ProductNew> allProducts = new List<ProductNew>();
var shopIdOfTheDay = 2;
allProducts = _db.Products
.Where(p => p.Category.ShopId == shopIdOfTheDay && p.Availability)
.OrderBy(p => p.Description)
.ToList();
//count the characters in the list here
int total = 0;
foreach (var value in allProducts)
{
string s = value.ToString();
int i;
i = s.Length;
total = total += i;
Console.WriteLine(total);
}
return new JsonResult { Data = allProducts, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
but the foreach loop is not working, because every string value is 109 characters long and that aint right.
If I break ath value.ToString is see that the value is +
{System.Data.Entity.DynamicProxies.ProductNew_7F6B12BDD7841029155EF84C6372688579A97D5AA4EA2378712AC64A67B25290} SeashellBrawlCorvee.Models.ProductNew {System.Data.Entity.DynamicProxies.ProductNew_7F6B12BDD7841029155EF84C6372688579A97D5AA4EA2378712AC64A67B25290}
So that aint right. There are multiple values in value, so that proberly why I cant cast it to String?
I tried doing an extra foreach. Like:
foreach (var value2 in value) {
string s1 = value2.ToString();
int i1;
i1 = s2.Length;
total = total += i1;
Console.WriteLine(total);
}
Something like this, but thats not working to because I get an error: foreach statement cannot operate on vairables of type 'PRoductNew'because 'ProductNew' does not contain a public definition for 'GetEnumerator'
So what to do?
Check string s = value.ToString();, the content of s may not be the result you want.
Because value is a class which is from ProductNew class, you can't get content through .ToString() method. If you want to count all characters, you should count every content of property in class, like this:
foreach (var value in allProducts)
{
string ID = value.ID.ToString();
string Category = value.Category.ToString();
string Description = value.Description.ToString();
total += (ID.Length + Category.Length + Description.Length);
Console.WriteLine(total);
}

Sort a List in which each element contains 2 Values

I have a text file that contains Values in this Format: Time|ID:
180|1
60 |2
120|3
Now I want to sort them by Time. The Output also should be:
60 |2
120|3
180|1
How can I solve this problem? With this:
var path = #"C:\Users\admin\Desktop\test.txt";
List<string> list = File.ReadAllLines(path).ToList();
list.Sort();
for (var i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
I got no success ...
3 steps are necessary to do the job:
1) split by the separator
2) convert to int because in a string comparison a 6 comes after a 1 or 10
3) use OrderBy to sort your collection
Here is a linq solution in one line doing all 3 steps:
list = list.OrderBy(x => Convert.ToInt32(x.Split('|')[0])).ToList();
Explanation
x => lambda expression, x denotes a single element in your list
x.Split('|')[0] splits each string and takes only the first part of it (time)
Convert.ToInt32(.. converts the time into a number so that the ordering will be done in the way you desire
list.OrderBy( sorts your collection
EDIT:
Just to understand why you got the result in the first place here is an example of comparison of numbers in string representation using the CompareTo method:
int res = "6".CompareTo("10");
res will have the value of 1 (meaning that 6 is larger than 10 or 6 follows 10)
According to the documentation->remarks:
The CompareTo method was designed primarily for use in sorting or alphabetizing operations.
You should parse each line of the file content and get values as numbers.
string[] lines = File.ReadAllLines("path");
// ID, time
var dict = new Dictionary<int, int>();
// Processing each line of the file content
foreach (var line in lines)
{
string[] splitted = line.Split('|');
int time = Convert.ToInt32(splitted[0]);
int ID = Convert.ToInt32(splitted[1]);
// Key = ID, Value = Time
dict.Add(ID, time);
}
var orderedListByID = dict.OrderBy(x => x.Key).ToList();
var orderedListByTime = dict.OrderBy(x => x.Value).ToList();
Note that I use your ID reference as Key of dictionary assuming that ID should be unique.
Short code version
// Key = ID Value = Time
var orderedListByID = lines.Select(x => x.Split('|')).ToDictionary(x => Convert.ToInt32(x[1]), x => Convert.ToInt32(x[0])).OrderBy(x => x.Key).ToList();
var orderedListByTime = lines.Select(x => x.Split('|')).ToDictionary(x => Convert.ToInt32(x[1]), x => Convert.ToInt32(x[0])).OrderBy(x => x.Value).ToList();
You need to convert them to numbers first. Sorting by string won't give you meaningful results.
times = list.Select(l => l.Split('|')[0]).Select(Int32.Parse);
ids = list.Select(l => l.Split('|')[1]).Select(Int32.Parse);
pairs = times.Zip(ids, (t, id) => new{Time = t, Id = id})
.OrderBy(x => x.Time)
.ToList();
Thank you all, this is my Solution:
var path = #"C:\Users\admin\Desktop\test.txt";
List<string> list = File.ReadAllLines(path).ToList();
list = list.OrderBy(x => Convert.ToInt32(x.Split('|')[0])).ToList();
for(var i = 0; i < list.Count; i++)
{
Console.WriteLine(list[i]);
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class TestClass {
public static void main(String[] args) {
List <LineItem> myList = new ArrayList<LineItem>();
myList.add(LineItem.getLineItem(500, 30));
myList.add(LineItem.getLineItem(300, 20));
myList.add(LineItem.getLineItem(900, 100));
System.out.println(myList);
Collections.sort(myList);
System.out.println("list after sort");
System.out.println(myList);
}
}
class LineItem implements Comparable<LineItem>{
int time;
int id ;
#Override
public String toString() {
return ""+ time + "|"+ id + " ";
}
#Override
public int compareTo(LineItem o) {
return this.time-o.time;
}
public static LineItem getLineItem( int time, int id ){
LineItem l = new LineItem();
l.time=time;
l.id=id;
return l;
}
}

List Re-Order is mixing up property values across list items

I have a strange situation happening or maybe it is my misunderstanding of lists, or it is one of those cases where you look at it too long and am missing something simple.
When I re-order a list in this example, it is mixing up column values across rows in the list. Here are some pics as an example as captured during debugging.
In the below pic, first note the list ordering done on 783. Next note the ItemId value of position [0] in the list. It is 1121.
Now after just advancing from line 793 to 798, note the values of inventoryNeed after it has been assigned the value from lstInventoryNeeds[i]. Note that its ItemId value is 1336! And further, the same list entry in position [0] also changed JUST the ItemId value from 1121 to 1336! What just happened?!?!
Now if I comment out the original list re-order code on line 783 and run it again, see this pic, captured just after the inventoryNeed assignment when i = 0 just as before, but there is no mix up in the ItemId value.
Any idea why this is happening?
Here is code for this section, but not sure how useful it is taken out of context:
private ActionConfirmation<int> AllocateContainersForNeedList(List<AllocationNeedViewModel> lstInventoryNeedsOriginal, int intUserId, bool failOnShortage)
{
//Sort by this so if first alloc attempt fails for the remnant,
//then add that qty to next Need in list (if exists) and attempt allocate for full sheets
var lstInventoryNeeds = lstInventoryNeedsOriginal;
//.OrderBy(x => x.ItemId)
//.ThenByDescending(x => x.IsRemnant)
//.ToList();
ActionConfirmation<int> allocateResult = ActionConfirmation<int>.CreateSuccessConfirmation("Allocation of Containers for Needs List Successful.",1);
bool firstOfTwoNeedsForSameItem = false;
AllocationNeedViewModel inventoryNeed;
for (int i = 0; i < lstInventoryNeeds.Count; i++)
{
inventoryNeed = lstInventoryNeeds[i];
//If this is an inventory tracked item, allocate it
if (boolIsInvenTrackedItem(inventoryNeed.ItemId))
{
//Allocate the required inventory for the need just created
if (lstInventoryNeeds.Count() > i + 1)
{
firstOfTwoNeedsForSameItem = inventoryNeed.IsRemnant && (lstInventoryNeeds[i + 1].ItemId == inventoryNeed.ItemId);
}
else
{
firstOfTwoNeedsForSameItem = false;
}
allocateResult =
AllocInvenForNeed(
inventoryNeed.FacilityId,
inventoryNeed,
intUserId,
0,
0,
!firstOfTwoNeedsForSameItem
);
//Create Allocation Error
if (!allocateResult.WasSuccessful)
{
//Check if original attempt was for a Remnant
if (firstOfTwoNeedsForSameItem)
{
//Add current remnant need to the next need for this item, which will then get allocated on the next loop
lstInventoryNeeds[i + 1].QtyNeeded = lstInventoryNeeds[i + 1].QtyNeeded + inventoryNeed.QtyNeeded;
}
else
{
return allocateResult;
}
}
} //if inventory tracked item
}//for loop
return allocateResult;
}
Here is a MCVE, but I can't replicate the problem...but maybe I'm missing some key element that would have an impact
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var lstInventoryNeedsOriginal = new List<AllocNeedVM>();
int b = 5;
for (int a = 0; a <= b; a++)
{
int c = b - a;
var allocNeed = new AllocNeedVM()
{
ItemId = c,
ItemDesc = "Item " + c.ToString(),
IsRemnant = false
};
lstInventoryNeedsOriginal.Add(allocNeed);
}
AllocNeedVM inventoryNeed;
Console.WriteLine("List before sort.");
for (int i = 0; i < lstInventoryNeedsOriginal.Count; i++)
{
inventoryNeed = lstInventoryNeedsOriginal[i];
Console.WriteLine("{0}, {1}, {2}", inventoryNeed.ItemId, inventoryNeed.ItemDesc, inventoryNeed.IsRemnant);
}
var lstInventoryNeeds = lstInventoryNeedsOriginal
.OrderBy(x => x.ItemId)
.ThenByDescending(x => x.IsRemnant)
.ToList();
Console.WriteLine("List after sort.");
for (int i = 0; i < lstInventoryNeeds.Count; i++)
{
inventoryNeed = lstInventoryNeeds[i];
Console.WriteLine("{0}, {1}, {2}", inventoryNeed.ItemId, inventoryNeed.ItemDesc, inventoryNeed.IsRemnant);
}
Console.ReadLine();
}
}
public class AllocNeedVM
{
public int ItemId { get; set; }
public string ItemDesc { get; set; }
public bool IsRemnant { get; set; }
}
}

How to count occurences of number stored in file containing multiple delimeters?

This is my input store in file:
50|Carbon|Mercury|P:4;P:00;P:1
90|Oxygen|Mars|P:10;P:4;P:00
90|Serium|Jupiter|P:4;P:16;P:10
85|Hydrogen|Saturn|P:00;P:10;P:4
Now i will take my first row P:4 and then next P:00 and then next like wise and want to count occurence in every other row so expected output will be:
P:4 3(found in 2nd row,3rd row,4th row(last cell))
P:00 2 (found on 2nd row,4th row)
P:1 0 (no occurences are there so)
P:10 1
P:16 0
etc.....
Like wise i would like to print occurence of each and every proportion.
So far i am successfull in splitting row by row and storing in my class file object like this:
public class Planets
{
//My rest fields
public string ProportionConcat { get; set; }
public List<proportion> proportion { get; set; }
}
public class proportion
{
public int Number { get; set; }
}
I have already filled my planet object like below and Finally my List of planet object data is like this:
List<Planets> Planets = new List<Planets>();
Planets[0]:
{
Number:50
name: Carbon
object:Mercury
ProportionConcat:P:4;P:00;P:1
proportion[0]:
{
Number:4
},
proportion[1]:
{
Number:00
},
proportion[2]:
{
Number:1
}
}
Etc...
I know i can loop through and perform search and count but then 2 to 3 loops will be required and code will be little messy so i want some better code to perform this.
Now how do i search each and count every other proportion in my planet List object??
Well, if you have parsed proportions, you can create new struct for output data:
// Class to storage result
public class Values
{
public int Count; // count of proportion entry.
public readonly HashSet<int> Rows = new HashSet<int>(); //list with rows numbers.
/// <summary> Add new proportion</summary>
/// <param name="rowNumber">Number of row, where proportion entries</param>
public void Increment(int rowNumber)
{
++Count; // increase count of proportions entries
Rows.Add(rowNumber); // add number of row, where proportion entry
}
}
And use this code to fill it. I'm not sure it's "messy" and don't see necessity to complicate the code with LINQ. What do you think about it?
var result = new Dictionary<int, Values>(); // create dictionary, where we will storage our results. keys is proportion. values - information about how often this proportion entries and rows, where this proportion entry
for (var i = 0; i < Planets.Count; i++) // we use for instead of foreach for finding row number. i == row number
{
var planet = Planets[i];
foreach (var proportion in planet.proportion)
{
if (!result.ContainsKey(proportion.Number)) // if our result dictionary doesn't contain proportion
result.Add(proportion.Number, new Values()); // we add it to dictionary and initialize our result class for this proportion
result[proportion.Number].Increment(i); // increment count of entries and add row number
}
}
You can use var count = Regex.Matches(lineString, input).Count;. Try this example
var list = new List<string>
{
"50|Carbon|Mercury|P:4;P:00;P:1",
"90|Oxygen|Mars|P:10;P:4;P:00",
"90|Serium|Jupiter|P:4;P:16;P:10",
"85|Hydrogen|Saturn|P:00;P:10;P:4"
};
int totalCount;
var result = CountWords(list, "P:4", out totalCount);
Console.WriteLine("Total Found: {0}", totalCount);
foreach (var foundWords in result)
{
Console.WriteLine(foundWords);
}
public class FoundWords
{
public string LineNumber { get; set; }
public int Found { get; set; }
}
private List<FoundWords> CountWords(List<string> words, string input, out int total)
{
total = 0;
int[] index = {0};
var result = new List<FoundWords>();
foreach (var f in words.Select(word => new FoundWords {Found = Regex.Matches(word, input).Count, LineNumber = "Line Number: " + index[0] + 1}))
{
result.Add(f);
total += f.Found;
index[0]++;
}
return result;
}
I made a DotNetFiddle for you here: https://dotnetfiddle.net/z9QwmD
string raw =
#"50|Carbon|Mercury|P:4;P:00;P:1
90|Oxygen|Mars|P:10;P:4;P:00
90|Serium|Jupiter|P:4;P:16;P:10
85|Hydrogen|Saturn|P:00;P:10;P:4";
string[] splits = raw.Split(
new string[] { "|", ";", "\n" },
StringSplitOptions.None
);
foreach (string p in splits.Where(s => s.ToUpper().StartsWith(("P:"))).Distinct())
{
Console.WriteLine(
string.Format("{0} - {1}",
p,
splits.Count(s => s.ToUpper() == p.ToUpper())
)
);
}
Basically, you can use .Split to split on multiple delimiters at once, it's pretty straightforward. After that, everything is gravy :).
Obviously my code simply outputs the results to the console, but that part is fairly easy to change. Let me know if there's anything you didn't understand.

Categories