在C#中随机化通用列表顺序的最佳方法是什么?我在一个列表中有一组有限的75个数字,我想为其分配一个随机顺序,以便为抽奖类型的应用程序绘制它们.
我想在绑定到a的下拉列表中添加"Select One"选项List<T>.
一旦我查询,我List<T>如何添加我的初始Item,而不是数据源的一部分,作为其中的FIRST元素List<T>?我有:
// populate ti from data
List<MyTypeItem> ti = MyTypeItem.GetTypeItems();
//create initial entry
MyTypeItem initialItem = new MyTypeItem();
initialItem.TypeItem = "Select One";
initialItem.TypeItemID = 0;
ti.Add(initialItem) <!-- want this at the TOP!
// then
DropDownList1.DataSource = ti;
Run Code Online (Sandbox Code Playgroud) 我上课Items了properties (Id, Name, Code, Price).
列表中Items填充了重复的项目.
例如:
1 Item1 IT00001 $100
2 Item2 IT00002 $200
3 Item3 IT00003 $150
1 Item1 IT00001 $100
3 Item3 IT00003 $150
Run Code Online (Sandbox Code Playgroud)
如何使用linq删除列表中的重复项?
我有一个数据DataReader,我希望将其转换为List<T>.对此有什么简单的解决方案?
例如,在CustomerEntity类中,我有CustomerId和CustomerName属性.如果我的DataReader将这两列作为数据返回,那么我该如何将其转换为List<CustomerEntity>.
我正在做一个反思项目,现在我被卡住了.如果我有一个可以容纳List的"myclass"对象,那么如果属性myclass.SomList为空,是否有人知道如何获取下面代码中的类型?
List<myclass> myList = dataGenerator.getMyClasses();
lbxObjects.ItemsSource = myList;
lbxObjects.SelectionChanged += lbxObjects_SelectionChanged;
private void lbxObjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Reflect();
}
Private void Reflect()
{
foreach (PropertyInfo pi in lbxObjects.SelectedItem.GetType().GetProperties())
{
switch (pi.PropertyType.Name.ToLower())
{
case "list`1":
{
// This works if the List<T> contains one or more elements.
Type tTemp = GetGenericType(pi.GetValue(lbxObjects.SelectedItem, null));
// but how is it possible to get the Type if the value is null?
// I need to be able to create a new object of the …Run Code Online (Sandbox Code Playgroud) 如何将以下枚举转换为字符串列表?
[Flags]
public enum DataSourceTypes
{
None = 0,
Grid = 1,
ExcelFile = 2,
ODBC = 4
};
Run Code Online (Sandbox Code Playgroud)
我找不到这个确切的问题,这个Enum to List是最接近但我特别想要的List<string>
我有一个包含几个属性的类(如果它有任何区别,则所有都是字符串).
我还有一个列表,其中包含许多不同的类实例.
在为我的类创建一些单元测试时,我决定循环遍历列表中的每个对象,然后循环遍历该对象的每个属性......
我以为这样做会很简单......
foreach (Object obj in theList)
{
foreach (Property theProperties in obj)
{
do some stufff!!;
}
}
Run Code Online (Sandbox Code Playgroud)
但这没有用!:(我收到此错误...
"foreach语句不能对'Application.Object'类型的变量进行操作,因为'Application.Object'不包含'GetEnumerator'的公共定义"
有没有人知道这样做的方法没有大量的ifs和循环或没有进入任何太复杂的东西?
什么是从列表中删除重复值的最快方法.假设List<long> longs = new List<long> { 1, 2, 3, 4, 3, 2, 5 };我很有兴趣使用lambda删除重复并返回:{1, 2, 3, 4, 5}.你的建议是什么?
请参阅下面的代码示例.我需要ArrayList一个通用列表.我不想用foreach.
ArrayList arrayList = GetArrayListOfInts();
List<int> intList = new List<int>();
//Can this foreach be condensed into one line?
foreach (int number in arrayList)
{
intList.Add(number);
}
return intList;
Run Code Online (Sandbox Code Playgroud) 假设我们有一个叫做Dog的类,它有两个字符串"Name"和"Id".现在假设我们有一张包含4只狗的清单.如果您想更改"Id"为"2"的Dog的名称,那么最好的方法是什么?
Dog d1 = new Dog("Fluffy", "1");
Dog d2 = new Dog("Rex", "2");
Dog d3 = new Dog("Luna", "3");
Dog d4 = new Dog("Willie", "4");
List<Dog> AllDogs = new List<Dog>()
AllDogs.Add(d1);
AllDogs.Add(d2);
AllDogs.Add(d3);
AllDogs.Add(d4);
Run Code Online (Sandbox Code Playgroud) c# ×10
generic-list ×10
.net ×2
list ×2
arraylist ×1
c#-4.0 ×1
datareader ×1
enums ×1
foreach ×1
lambda ×1
linq ×1
properties ×1
reflection ×1