List that contains list that contains list and so on in c# [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
This is code:
public class Comment
{
public DateTime CreateAt { get; set; }
public Comment ParentComment { get; set;}
public List<Comment> SubComments { get; set; }
public string Text { get; set; }
}
I'am trying to do something like on facebook. You can comment every single comment, so there will be like a tree of comments.. I'am having trouble with trying to display all the comments, precisely text. I can't figure out how to do that. If anyone could help I'll appreciate it!

You can use recursion for process each comment and their sub comments for example:
public void checkComment(Comment comment)
{
//Check if the comment is valid
if (comment != null)
{
//Do whatever you want to do with your comment for example print to console
Console.WriteLine(String.Format("Comment: {0}", comment.Text));
//Check if i have any sub comments
if (comment.SubComments.Count > 0)
{
//Process each sub comment (recursive)
comment.SubComments.ForEach(x => checkComment(x));
}
}
}

Related

C# how does this code work? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Public class form1
{
List<Data> dataInfo = new List<Data>();
private void UpdateBinding()
{
supplierBox.DataSource = dataInfo;
supplierBox.DisplayMember = "Supplier";
}
}
I have another class called Data:
public class Data
{
public string Supplier { get; set; }
}
This is able to update my textBox named supplier on Visual Studio with data from my SQL Server that I have grabbed. How would I access the data from my SQL Server if I just wanted to put it in a variable like var?
supplier.Text = dataInfo.FirstOrDefault().Supplier;
would place the first element from your list of suppliers into your text box named "suppliers". If you don't have any items in your list of dataInfo then you would get a NullReferenceException.

Have Required Attribute apply to property in one view and not another? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Say I have a class called Test and I have a Create and Edit view. Say this class is incredibly simple
public class Test
{
[Required]
public string str { get; set; }
}
Is it possible to remove the required attribute when a user is editing this object?
ViewModels are there for this. One for Create and one for Edit.
You should use a ViewModel for this, as you need View Specific Models here :
public class CreateTestViewModel
{
[Required]
public string str { get; set; }
}
and:
public class EditTestViewModel
{
public string str { get; set; }
}
You might want to read about What is ViewModel in MVC and How to use ViewModel in MVC

loop to find parent id [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it possible in C# to carry out a loop to find the parent id for a particular page?
Here's the scenario: I'm developing a breadcrumb. For each page I need the parent Id, so I can use that to populate a list with a url and a page title.
1st page --- Parent page --- Grandparent page
I need to be able to do a loop to gather the parent id, then that will populate the list which goes through and finds the new parent page parent id (basically for the grandparent page) and then populates the list until there aren't any more.
At the moment the current page data is stored in var pageInSiteMap:
var overview = new List<PageDataModel>();
pageInSiteMap // A get command is used to place the data in here
if(pageInSiteMap != null && pageInSiteMap.ParentId.HasValue)
{
var parent = allpages.Data.Items.FirstOrDefault(x => x.Id == pageInSiteMap.ParentId
var parentEntry = new PageDataModel{
pageUrl = parent.Url, pageTitle = parent.Title
};
overview.Add(parentEntry);
}
I am going to take a stab at this and assume you need recursion, here is a quick example I wrote:
public class Page
{
public int PageId { get; set; }
public string PageTitle { get; set; }
public Page Parent { get; set; }
}
public class Class1
{
public List<Page> GetPages(Page currentPage)
{
var ret = new List<Page> {currentPage};
if(currentPage.Parent != null)
ret.AddRange(GetPages(currentPage.Parent));
return ret;
}
}

C# parse string into object [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
For the following text:
Item 1:
student-pay
history 320.00
math 500.10 extra
Item 2:
history 220.00
math 200.10
I would like to parse this text into an Item class. Is there a more efficient way to do this?
I have the following:
var result = input.Split(
new[] { "item", "Item" },
StringSplitOptions.RemoveEmptyEntries);
foreach (string item in result)
{
var lineItems = item.Split(
new[] { Environment.NewLine },
StringSplitOptions.RemoveEmptyEntries);
foreach (var lineItem in lineItems.Skip(1)) // remove ":"
{
// split line again
// check if numeric
}
}
public class Course
{
public bool StudentPay { get; set; }
public ICollection<CourseItem> JobItems { get; set; }
}
public class CourseItem
{
public string Name { get; set; }
public decimal Cost { get; set; }
public bool Extra { get; set; }
}
I want to end up with List<Items>
Given that data format your stuck with having to hand code a one off solution.
A much better approach is to use a json format. Get Newtonsoft.json. It's a one liner to serialize it and a one liner to deserializer. Add another line for File.Write and File.Read and away you go.
Use JsonConvert.Serialize and JsonConvert.Deserialize, will work most classes (unlike xmlserializer).
Json is probably a better solution but it require having the control of the input string being formatted to json format.
If you have to use your existing string as input, I suggest you to use RegEx to parse each line for the object creation. Regex is the old-fashion way to tackle your problem.

Display datagrid based on one filter in wpf [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have datagrid and one value slider.I have three columns .Three columns like name,age and telephone number.Filter value is set to age.If i change slider value that is filter(age) based on filter DataGrid needs to display the data.I am using observable collection.
I think i understood what you want exactly.
Assuming that the name of your Data Grid is "MyDataGrid" and it's data source is bound to a class "MyDataGridItem" with this structure
public class MyDataGridItem
{
public string Name { get; set; }
public int Age { get; set; }
}
Subscribe the ValueChanged event of the slider and get the value and use LINQ where query to filter your results.
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var sliderValue = (int) MySlider.Value;
MyDataGrid.ItemsSource = students.Where(item =>item.Age<sliderValue);
}

Categories

Resources