I want to loop through three List<string>. I'm sending ajax request containing 3 parameter as Array.
This is my WebMethod.
public string saveEachTask(string imageData, string desc, string tid)
{
var imglist = imageData.Split(',').ToList();
var desclist = desc.Split(',').ToList();
var idlist = tid.Split(',').ToList();
// here i want a foreach loop for above three list
return "Saved Succesfully";
}
Edit: in loop how can I identity which string is for img, desc and id
Any Help appreciated.
Perhaps something like this:
foreach (var list in new[] {imglist, desclist, idlist})
{
foreach (var s in list)
{
}
}
Use one loop and each iteration process each list.
public string saveEachTask(string imageData, string desc, string tid)
{
var imglist = imageData.Split(',').ToList();
var desclist = desc.Split(',').ToList();
var idlist = tid.Split(',').ToList();
int maxLength = Math.Max(imglist.Count, Math.Max(desclist.Count, idlist.Count));
for (int i = 0; i < maxLength; ++i)
{
string imgItem = (i < imglist.Count ? imglist[i] : null);
string descItem = (i < desclist.Count ? desclist[i] : null);
string idItem = (i < idlist.Count ? idlist[i] : null);
// TODO: Process imgItem, descItem, idItem
}
return "Saved Succesfully";
}
If you need to handle everything the same way in your loop, you could try:
foreach(var s in imglist.Union(desclist.Union(idlist)))
{
// do something with the string
}
Related
List<string> lst = new List<string>() { "mahdi","arshia","amir"};
int a = 0;
var list_mian = lst[a];
for (int i = a; i <Convert.ToInt16(list_mian); i++) //Additional information: Input string was not in a correct format.
{
MessageBox.Show(lst.IndexOf(lst[0]).ToString());
}
I want to show a list item's index in a loop, for example of mahdi's index is 0 and amir's index is 2 i wanna show their index respectively in a "for" loop and i give an error that i show that in the code part
Your trying to convert an integer to a string and then use that as a range on the for loop just use .count and compare it to the name attached to that index of the list. Hope you find this useful.
public static int? findPerson(string name)
{
List<string> lst = new List<string>() { "mahdi", "arshia", "amir" };
int? result = null;
for (int i = 0; i < lst.Count; i++) //Additional information: Input string was not in a correct format.
{
if (lst[i] == name)
{
result = i;
}
}
return result;
}
static void Main(string[] args)
{
var index = findPerson("arshia");
if (index == null)
{
Console.WriteLine("PersonNotFound");
}
else {
Console.WriteLine("Index of " + index.ToString());
}
}
You can do it with IndexOf it returns the index or -1 when there is no item.
List<string> list = new List<string>() { "mahdi", "arshia", "amir" };
var indexOfAmir = list.IndexOf("amir"); // 2
var indexOfMax = list.IndexOf("max"); // -1
I am trying to return that data from the foreach "hostedId" Can someone help?
public static string GetHostedRecordSet()
{
var request = new ListHostedZonesRequest()
{
MaxItems = "1"
};
var list = client.ListHostedZones(request);
foreach (var hostedId in list.HostedZones)
{
Console.WriteLine("\n Hosted ID is:");
Console.Write(hostedId.Id);
}
return hostedId;
}
It depends. If you want to return the first element:
return list.HostedZones.First().Id; // Not in a loop!
If you want to return several items, change the signature of the method:
public static IEnumerable<string> GetHostedRecordSet()
{
var request = new ListHostedZonesRequest()
{
MaxItems = "1"
};
var list = client.ListHostedZones(request);
return list.HostedZones
.Select(z => z.Id);
}
If you want to return all values as a single string you can concatenate them with a delimiter, such as ',':
public static string GetHostedRecordSet()
{
var request = new ListHostedZonesRequest()
{
MaxItems = "1"
};
var list = client.ListHostedZones(request);
StringBuilder result = new StringBuilder();
foreach (var hostedId in list.HostedZones)
{
result.Append(hostedId.Id).Append(",");
}
return result.ToString(0, Math.Max(0, result.Length - 1);
}
Is there any possiblity to extract only the numbers from a GUID? I'm trying to achieve this because I don't want to display the GUID in a form, but the numbers within, which also are unique.
You can use IsDigit to get only the numbers
var guidstring = Guid.NewGuid().ToString("N");
var getNumbers = (from t in guidstring
where char.IsDigit(t)
select t).ToArray();
DEMO
public string GetNumbersFromGuid(Guid Item)
{
var result = string.Empty;
var guidArray = Item.ToString().ToCharArray();
int n;
foreach (var item in guidArray)
{
if (int.TryParse(item.ToString(), out n) == true)
{
result += item.ToString();
}
}
return result;
}
Call it like:
var MyValue = GetNumbersFromGuid(Guid.NewGuid());
I need to form the string in for loop.I get the one by one value in "name" variable.Now I need all the value in namevalues variable? the listbox having Pieter,John,Joseph Items.
How can i do this?
for (int i = 0; i < 2; i++)
{
string name = Listboxs.Items[i].ToString();
string namevalues = ??;
}
Expected Output is : Pieter*John*Joseph
Since Listboxs.Items returns ListBox.ObjectCollection which implements IEnumerable interface, you can just use string.Join without for loop like;
string.Join("*", Listboxs.Items.Cast<string>());
should return
Pieter*John*Joseph
Use string.Join with Cast()
var namevalues = string.Join("*", Listboxs.Items.Cast<string>().ToArray());
StringBuilder namevalues = new StringBuilder("");
for(int i =0; i<2; i++)
{
if(namevalues.Length >0 )
{
namevalues.Append("*" + Listboxs.Items[i].ToString());
}
else
{
namevalues.Append(Listboxs.Items[i].ToString());
}
}
I have the following class definition:
public static string SplitString(string someText)
{
var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
foreach (var i in Enumerable.Range(0, queryArray.Count - 1)) {
// Some code
}
}
The problem is that queryArray.Count is giving me the following error:
The property 'System.Array.Count' cannot be used in this context because the get accessor is inaccessable.
What am i missing here?
You may try the Length property instead:
public static string SplitString(string someText)
{
var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
foreach (var i in Enumerable.Range(0, queryArray.Length - 1)) {
// Some code
}
}
Also your code would probably have been more readable if it was written like this:
public static string SplitString(string someText)
{
var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
for (var i = 0; i < queryArray.Length; i++) {
// Some code
}
}
or like this:
public static string SplitString(string someText)
{
var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
foreach (var item in queryArray) {
// Some code
}
}
Regex.Split returns an array, which doesn't define a Count property. Use Length instead:
public static string SplitString(string someText)
{
var queryArray = Regex.Split(someText, "\\s+(?=\\w+)");
foreach (var i in Enumerable.Range(0, queryArray.Length - 1)) {
// Some code
}
}
You can try with Length property
Why worry about an index when the Select extension will get it for you:
var data = Regex.Split("someText other", "\\s+(?=\\w+)")
.Select((itm, indexer) => string.Format("{0} is index {1}", itm, indexer));
/* Data has 2 strings in the list:
someText is index 0
other is index 1
*/