0

I want to know if I can access an item of my List and assign it to a textbox?

public static List<Product> detailsList = new List<Product>();

Product is my class generated by using LINQ to SQL with fields Name,Code,Details i.e my List contains the items(Name,Code,Details). I want to access the value of the item Details from the list and assign it to a textbox . Something like:

txtDetails.Text = detailsList.Details.ToString()

8
  • This list contains number of items. Which Item list detail do you wanna show Commented Jan 15, 2016 at 7:09
  • You want to join the Detail together? Commented Jan 15, 2016 at 7:10
  • @User2012384 No, I just want to access one of the items of my List so I can assign it to a textbox Commented Jan 15, 2016 at 7:20
  • @HumaAli check RoteS's answer Commented Jan 15, 2016 at 7:22
  • How do you want to filter the list to get the single item? Should it be based on Name or Code? Do you want the first item, last, or a particular index? Do you want to skip a certain number of items? Etc. Commented Jan 15, 2016 at 7:24

4 Answers 4

4

If you want the details of 1 item:

var detailsList = new List<TaskDto>();
// add items

// this if you know the corect index
txtDetails.Text = detailsList[0].Details;

// if you need to query for example for the item with the correct Id
txtDetails.Text = detailsList.FirstOrDefault(p => p.Id == 1).Details;
Sign up to request clarification or add additional context in comments.

Comments

0

You can select first or default from the item based on some criteria, like the name.

public static List<Product> detailsList = new List<Product>();
var prod = detailsList.FirstOrDefault(p => p.Name == 'Ana');

if(prod != null) {
    txtDetails.Text = prod.Details;
}

Comments

0

For all items:

var joinSymbol = ", ";
txtDetails.Text = string.Join(joinSymbol, detailsList.Select(detail => detail.Details));

For first item if Details:

txtDetails.Text = detailsList.FirstOrDefault()?.Details ?? string.Empty;

Comments

-1

use string.Join() function.

txtDetails.Text = string.Join(",", detailsList.Select(e => e.Details)).ToList());

2 Comments

string.Join takes parameters in reversed order.
@DovydasSopa you are right, just edited my post. thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.