Remove last character in a list [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I am building a list from a dataset as 12345_9876,125675_0987,
I try to remove the last , from the list like we do for strings..
value.TrimEnd(',');
Can we do similar thing for a list?

I hope this will help you?
var lastElement = yourList.ElementAt<string>(yourList.Count - 1).TrimEnd(',');
yourList.RemoveAt(yourList.Count - 1);
yourList.Insert(yourList.Count,lastElement);

Think you just want this?:
List<string mylistfromdataset = // I dunno how you populated it
var newlist = mylistfromdataset.Select(x=> x.TrimEnd(',')).ToList();
EDIT after the poster cleared the question, you could use this 2 line code to make a new list where the last string the , is trimmed.
var list = listofstring.Take(listofstring.Count - 1).ToList();
list.Add(listofstring.Last().TrimEnd(','));
(Make a list excluding the last item. then trim the last item with , at the end and at it to the new list.)

Just use String.Join:
String delimitedList = String.Join(",", yourList);

int i=value.LastIndexOf(",");
if(i!=-1)
value=value.Remove(i,1);

Related

How to remove negative sign from LINQ output [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
i have following linq used in my application
var FinalSubExpired = subExpired.Where(e => (DateTime.Now - Convert.ToDateTime(e.AreasOfLawTillDate)).TotalDays <= 30 ).ToList();
which returns the total days with negative values, i need to remove that negative sign from that total days. how can i do that by modifying this linq?
Please help.
var FinalSubExpired = subExpired.Where(e => Math.Abs((DateTime.Now - Convert.ToDateTime(e.AreasOfLawTillDate)).TotalDays) <= 30).ToList();
that should do the trick
try and use Math.Abs inside your Linq query, as:
var FinalSubExpired = subExpired.Where(e => (Math.Abs(DateTime.Now - Convert.ToDateTime(e.AreasOfLawTillDate)).TotalDays) <= 30 ).ToList();

Looping through an array objects within another array of objects [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have an array of object called objStud within it has another array
how do I loop and display them?
Here is an image that you can see how objStud is like
Just use nested foreach statements
foreach (Student stud in objStud)
{
foreach (Fee in stud.Fees)
{
// Do something with stud and/or fee
}
}
You can use SelectMany to flatten first:
foreach(var fee in objStud.SelectMany(x => x.Fees))
{
}
You can use "foreach" in looping and displaying the values. This is a very easy and good example on how to access the data that you want to display.
This will also teach you the concept behind array. Have fun!

C# Creating a list for events that did not occur today [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I`m making an application for an animal shelter, and each time a dog is added to the animal list it has to have the date it last went for a walk. Part of the assignment is to show a list of dogs that are not walked out TODAY. Any ideas?
OK, assuming that you have a "LastWalked" column in your data structure you want something like:
var dogsNotWalked = allDogs.Where(d => d.LastWalked < DateTime.Today);
It would help if you'd try something before asking for help. Not to mention showing your work. It's not exactly difficult, a one-liner, in fact. Try something like this:
public IEnumerable<Dog> Dogs { get ; set ; }
public IEnumerable<Dog> FindDogsNotWalkedRecently( DateTime referenceDate )
{
return Dogs.Where( dog => dog.LastWalkedAt < referenceDate ) ;
}

Can't use 'contains' in LINQ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
string last = url.Substring(url.LastIndexOf('/') + 1);
var provisionedSiteRequestRep = provisioningRequestRepository.SelectFirst(new WhereSpecification<ProvisioningRequest>(result => result.SiteUrl.Contains(last.ToString())));
Some time i am getting the null values of last.tosting() so i am getting exception for this code how to resolve this?
You are facing problem on this line
(result => result.SiteUrl.Contains(last.ToString());
Can you please check that SiteUrl is type of string otherwise it not going to work for you.
because last is type of string and Contains is method supported by string type ...
or
otherwise last need to be enumebrable collection and siteurl also enumerable collection than and only than Contains is supported

Howto split genericList items with delimiter? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I cant figure out howto split items from my genericList to two seperate parts with delimiter option?
List<string> fileLines = File.ReadAllLines(fileName).Skip(4).ToList();
foreach (var item in fileLines)
{
values = item.Split(' ');
sList.Add(values[3].Substring(2).Trim());
}
My sList looks like this:
10.5 5.5
7.2 2.5
-0.1 3.0
-1.1 3.3
and so on .......... totaly 8760 rows in my List.
What I want to do is to split each row from the List to two seperate parts so I can count the min, max and average on thoose values.
(each value is meant to represent the temperature, so double)
Any help would be appreciated !!! Thanx
So why don't you use this one
List<string> fileLines = File.ReadAllLines(fileName).Skip(4).ToList();
foreach (var item in fileLines)
{
values = item.Split(' ');
string[] vl=values[3].Substring(2).Trim().Split('\t');
sList1.Add(vl[0]);
sList2.Add(vl[1]);
}

Categories

Resources