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 1 year ago.
Improve this question
I have a binding list with streets. It includes a name and an ID. I want to show the names of the streets in a combobox. The thing is I only want street names to show with a specific start letter. Like my combobox should only show street names that start with the letter S.
If it helps, I use Windows forms and I try to use the Mvvm model.
Does anyone have the link for a solution? I got it to work that my combobox shows all the street names but not the specific names I want.
This simple program makes a list of streets and adds two streets to this list.
Then all streets starting with "S" are put into "streetsStartingWithS". This is done with LINQ. Then these streets are printed.
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
List<Street> streets = new List<Street>();
streets.Add(new Street("Superstreet", 1));
streets.Add(new Street("Anotherstreet", 2));
IEnumerable<Street> streetsStartingWithS = streets.Where(s => s.Name.StartsWith("S"));
foreach (Street street in streetsStartingWithS){
Console.WriteLine(street.Name);
}
}
}
public class Street{
public string Name {get;set;}
public int Id {get;set;}
public Street (string name, int id){
Name = name;
Id = id;
}
}
Output:
Superstreet
Related
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.
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
I will try to make clear my problem :
Let's assume that i have a coach company and my buses has a few different service lines.
My aim is to separately store data of services for per service in in a string array "string[]".
As my thought, the name of array must be consist of (Date time + service number)
The dream was to put all passenger and the number of sold coaches information into a string[]
For instance :
//** "2303201601" >>> day + month + year + service number
string[] "2303201601" = new string[44]; // of course doesn't work :P
so i tried my chance with the following code :
public static Array new_list (string name_list, int size)
{
string [] name_list = new string[size];
return name_list;
}
Please help me :D ?
Ok I think this is what you want, though your question is not clear. Build an object to hold all your data properly. Strings make bad objects
public class Bus
{
public Bus()
{
ServiceNumbers = new List<string>();
}
public DateTime Date {get; set;}
public int ServiceNumber {get; set;}
public List<string> ServiceNumbers {get; set;}
}
Now just use it
List<Bus> buses = new List<Bus>();
Bus busx = new Bus()
{
Date = new DateTime(year, month, day),
ServiceNumber = "X"
}
busx.ServiceNumbers.Add("servicenumberx");
buses.add(busx);
I'm presuming here that the string is just a storage mechanism and there is no special reason for it. i.e. you have another piece of technology that holds string only, etc.
You can use Dictionary. It will be like this:
var someVar=new Dictionary<string, string[]>();
someVar.Add("date+servnum", yourarray);
var yourArray = someVar["date+servnum"];
You can do this
public class service{
private string name;
private string[] info;
public service(string name,int size){ this.name=name; info=new string[size];
public getInfo(){ return info;}
}
And then, if you need an array of services you can do it:
List<services> allServices=new List<services>();
I do not know if I understand the problem correctly
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 7 years ago.
Improve this question
combobox.Items.Add(object item), so why we can use combobox.Items.Add("Some text"), although "Some text" is string, not object?
A string is a reference type, so it is an object. What combobox does is then call the .ToString() method of the object it receives to transform it in a string (to show it). The ToString() of a string simply returns the string.
Why is the Add() done in that way?
Let's look at an example using WinForms:
// A simple Person class, Name + Surname
public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
public override string ToString()
{
return Name + " " + Surname;
}
}
// cb is your ComboBox
// Three persons in the ComboBox
cb.Items.Add(new Person { Name = "Bill", Surname = "Gates" });
cb.Items.Add(new Person { Name = "Larry", Surname = "Ellison" });
cb.Items.Add(new Person { Name = "Peter", Surname = "Norton" });
// and then somewhere the user selects an item in the combobox
// and then we can
Person selectedPerson = (Person)cb.SelectedItem;
string selectedPersonDescription = cb.SelectedText;
You can retrieve not only the description of the selected item, but the "whole" selected item! You see the advantage? (as I said before, the "description" for the items is automatically calculated by the ComboBox class by using the ToString() method)
So clearly the ComboBox is saving the "whole" objects you Add(...) to it, and calculating the descriptions (with ToString()) to show to the user.
It's thanks to polymorphism. Because string derives from object, you can pass it to the method, as well as any other instance of type deriving from object (which is almost everything).
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);
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is there a way to get the specific items from the list of a model without using a foreach of the list? like if you return a List<model> holding a model with a few differnt items to specifically call those item.
If you want a list of a specific item that's within your list then you can use LINQ to return those items, for example:
var customerNames = customersList.Select(customer => customer.Name);
This would give you a string collection with all of your customer names equal to the amount that was in your original list.
Or if you would like to retrieve a number of different items from your model list then you may want to look into creating a class specifically for stripping out those items, for example:
public class CustomerContactDetails
{
public string Address { get; set; }
public string Email { get; set; }
public string Telephone { get; set; }
}
...
var contactDetails = customersList.Select(customer => new CustomerContactDetails { Address = customer.Address, Email = customer.Email, Telephone = customer.Telephone });
You can use C# LambdaExpression, like this:
var yourModel = modelList.Where(x => x.Id = 12).Select(x).First();