我有两个List<T>对象:
例如:
清单1:
ID,填充了Id的值,值为空,它包含1到10的说法ID.1
,""
2,""
......
10,""
列表2:
ID,值和其他属性都填充了值,但此列表在ID方面是列表1的子集.(例如只有3项)
2,67
4,90
5,98
我想要的是合并列表1,但具有更新的值.有没有人有任何好的扩展方法可以执行此操作或执行此操作的任何elegent代码.最终的清单应该是:
ID,值
1,""
2,67 //值列表2
3,""
4,90
5,98
6,""
......
10,""
我读了很多关于这个主题的帖子; 其中以及最近的.NET - 将通用集合转换为数据表.不幸的是,一切都无济于事.
我有一个通用的结构集合:
Private Structure MyStruct
Dim sState as String
Dim lValue as Long
Dim iLayer as Integer
End Structure
Dim LOStates As New List(Of MyStruct)
Run Code Online (Sandbox Code Playgroud)
我需要用这个结构列表填充DataTable,但不知道如何去做.我在Visual Studio 2008中使用vb.net.
任何见解将不胜感激
我正在List<Image>从大约250张图片的文件夹中加载一个.我做了一个DateTime比较,加载这250个图像需要整整11秒.那个地狱很慢,我非常想加快速度.
图像在我的本地硬盘上,甚至不是外部硬盘.
代码:
DialogResult dr = imageFolderBrowser.ShowDialog();
if(dr == DialogResult.OK) {
DateTime start = DateTime.Now;
//Get all images in the folder and place them in a List<>
files = Directory.GetFiles(imageFolderBrowser.SelectedPath);
foreach(string file in files) {
sourceImages.Add(Image.FromFile(file));
}
DateTime end = DateTime.Now;
timeLabel.Text = end.Subtract(start).TotalMilliseconds.ToString();
}
Run Code Online (Sandbox Code Playgroud)
编辑:是的,我需要所有的照片.我正在计划的事情是将每个中心的30个像素柱取出并从中制作出新的图像.有点像360度的图片.只是现在,我只是用随机图像进行测试.
我知道可能有更好的框架可以做到这一点,但我需要首先工作.
EDIT2:切换到秒表,差别只有几毫秒.还尝试使用Directory.EnumerateFiles,但没有任何区别.
编辑3:我在32位Win7客户端上运行.NET 4.
我试图将列表父项之类的通用列表绑定到ComboBox.
public Form1()
{
InitializeComponent();
List<Parent> parents = new List<Parent>();
Parent p = new Parent();
p.child = new Child();
p.child.DisplayMember="SHOW THIS";
p.child.ValueMember = 666;
parents.Add(p);
comboBox1.DisplayMember = "child.DisplayMember";
comboBox1.ValueMember = "child.ValueMember";
comboBox1.DataSource = parents;
}
}
public class Parent
{
public Child child { get; set; }
}
public class Child
{
public string DisplayMember { get; set; }
public int ValueMember { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的测试应用程序时,我只看到:"ComboBindingToListTest.Parent"显示在我的ComboBox中,而不是"显示它".如何通过一个级别或更深层的属性将ComboBox绑定到通用列表,例如child.DisplayMember?
阿道夫先生,先谢谢你
以我个人的编码风格所属Enumerable.OfType<T>().我在任何地方使用它都有点意义.特别是IEnumerable<T>允许强大的linqToObject功能.我讨厌ObjectCollections的"类型不安全"循环,例如下面的示例.现在我有一些关于循环这些"UnGenericCollections"的问题.
ConfigurationSectionCollectionHttpModuleCollectionSPBaseCollectionSPFieldCollectionArrayList问题1:如果我将此转换ArrayList为Enumerable<T>与简单的foreach/if-checks相比有多大的额外循环?
var ark = new ArrayList();
ark.Add(new Human());
ark.Add(new Human());
ark.Add(new Animal());
Run Code Online (Sandbox Code Playgroud)
代替:
foreach (object passenger in ark)
{
if (passanger is Human) { }
if (passanger is Animal) { }
}
Run Code Online (Sandbox Code Playgroud)
我用:
foreach (var human in ark.OfType<Human>())
{
}
foreach (var animal in ark.OfType<Animal>())
{
}
Run Code Online (Sandbox Code Playgroud)
问题2:在foreach循环中使用不同类型的变量时,将使用哪种投射/转换方式?这是一种语言功能还是开箱即用?
foreach (Human human in ark) { }
Run Code Online (Sandbox Code Playgroud)
谢谢你忍受我可怕的英语.最好的问候,本杰明
我正在尝试使用ListItems列表将项目添加到下拉列表,但它没有获取值(仅文本).
代码如下(从我实际使用的代码中简化):
PositionDropDown.DataSource = GetPositionOptions();
PositionDropDown.DataBind();
private List<ListItem> GetPositionOptions()
{
List<ListItem> items = new List<ListItem>();
items.Add(new ListItem("",""));
items.Add(new ListItem("Top (main)", "TOP"));
items.Add(new ListItem("Bottom (full width)", "BTM"));
items.Add(new ListItem("Bottom Left", "MIL"));
items.Add(new ListItem("Bottom Middle", "MID"));
return items;
}
Run Code Online (Sandbox Code Playgroud)
但是,呈现的HTML缺少ListItem构造函数的第2个参数中指定的值:
<option value=""></option>
<option value="Top (main)">Top (main)</option>
<option value="Bottom (full width)">Bottom (full width)</option>
<option value="Bottom Left">Bottom Left</option>
<option value="Bottom Middle">Bottom Middle</option>
Run Code Online (Sandbox Code Playgroud)
为什么它不使用指定的"值"而只是在呈现HTML时重复"名称"?我究竟做错了什么?
我花了一些时间寻找处理通用对象的替代方法,我看到类似于我的问题,但不是我想的那么具体?协议缓冲区有多种标量类型我可以使用,但它们大多是原始的.我希望我的消息灵活,并且能够拥有某种列表的字段.
假设我的.proto文件看起来像这样:
message SomeMessage
{
string datetime = 1;
message inputData // This would be a list
{
repeated Object object = 1;
}
message Object
{
? // this need to be of a generic type - This is my question
// My work around - Using extentions with some Object
//List all primitive scalar types as optional and create an extension 100 to max;
}
message someObject //some random entity - for example, employee/company etc.
{
optional string …Run Code Online (Sandbox Code Playgroud) 我有BindingList对象,它与DataGridView一起使用.
BindingList<FilesToProcessDataModels> Listfiles = new BindingList<FilesToProcessDataModels>();
dataGridFiles.DataSource = Listfiles;
Run Code Online (Sandbox Code Playgroud)
我想按照Where中的给定条件过滤项目列表.如下:
dataGridSheets.DataSource = Listfiles.Where(i => i.Status == FileStatus.NotProcessed).ToList();
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常,但我想将过滤器resut分配给相同类型的对象[Listfiles]而不是datagrid,
当我这样做时:
Listfiles = Listfiles.Where(i => i.Status == FileStatus.NotProcessed).ToList();
Run Code Online (Sandbox Code Playgroud)
它给了以下的erorr
[无法将类型'System.Collections.Generic.List隐式转换为BindingList']
如何将通用列表转换为BindingList<FilesToProcessDataModels>?
我正试图通过我的班级列表DbParameter.可能在我的存储过程中定义了表类型.
现在,我没有得到如何传递List<>到存储过程,因为定义的表类型Tables只接受.
在这里,我正在采用我的方法.
public static AddCustomer(List<Customer> customer)
{
List<DbParameter> lstDbParameters = null;
try
{
#region Set the Parameters
lstDbParameters = new List<DbParameter>();
SqlParameter dbAcceptedBillDetails = new SqlParameter("@Customers",
customer);
dbAcceptedBillDetails.SqlDbType = SqlDbType.Structured;
lstDbParameters.Add(dbAcceptedBillDetails as DbParameter);
lstDbParameters.Add(CDDAC.MakeDbParameter(dbProvider,
"@ErrorMessage",
DbType.String,
null,
500,
ParameterDirection.Output));
#endregion
//Call the static ExecuteNonQuery method.
CDDAC.ExecuteNonQuery(dbProvider,
connectionString,
"AddCustomer",
CommandType.StoredProcedure,
lstDbParameters.ToArray());
}
catch (Exception ex)
{
throw;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到这样的错误:
无法从列表
1 to a IEnumerable1 转换参数值.
我知道我可以将此列表转换为DataTable然后在存储过程中传递它,但它似乎很耗时.:(
在下面的代码Java中,我创建了一个列表nums.我可以在声明期间分配另一个列表.但是除了null.之外不能添加新项目.那么,这是否意味着nums只读?为什么?是否可以在该列表中添加新项目?
List<Integer> ints = new ArrayList<Integer>();
ints.add(1);
ints.add(2);
List<? extends Number> nums = ints;
nums.add(3.14); //Generates error
nums.addAll(ints); //Generates error
nums.add(null); //works
System.out.println(nums.get(0)); //works
Run Code Online (Sandbox Code Playgroud)
我已经通过了这个链接.我无法得到确切的理由.