C#通用列表<;T>;更新项目
我正在使用一个C#通用列表<;T>;更新项目,c#,generic-list,C#,Generic List,我正在使用一个列表,我需要更新列表中的对象属性 最有效/更快的方法是什么?我知道,随着列表的增长,List的索引扫描速度会变慢,List并不是进行更新的最有效的集合 这很可悲,最好是: 删除匹配对象,然后添加新对象 扫描列表索引,直到找到匹配的对象,然后更新对象的属性 如果我有一个集合,让我们使用IEnumerable,我想将该IEnumerable更新到列表中,那么最好的方法是什么 存根代码示例: public class Product { public int ProductI
列表
,我需要更新列表中的对象属性
最有效/更快的方法是什么?我知道,随着列表的增长,List
的索引扫描速度会变慢,List
并不是进行更新的最有效的集合
这很可悲,最好是:
- 删除匹配对象,然后添加新对象
- 扫描列表索引,直到找到匹配的对象,然后更新对象的属性
- 如果我有一个集合,让我们使用IEnumerable,我想将该IEnumerable更新到列表中,那么最好的方法是什么
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Category { get; set; }
}
public class ProductRepository
{
List<Product> product = Product.GetProduct();
public void UpdateProducts(IEnumerable<Product> updatedProduct)
{
}
public void UpdateProduct(Product updatedProduct)
{
}
}
公共类产品
{
public int ProductId{get;set;}
公共字符串ProductName{get;set;}
公共字符串类别{get;set;}
}
公共类产品存储库
{
List product=product.GetProduct();
public void UpdateProducts(IEnumerable updatedProduct)
{
}
公共无效更新产品(产品更新产品)
{
}
}
效率到底是什么
除非有成千上万的项目在做foreach、for或任何其他类型的循环操作,否则它们很可能只在千秒内显示差异。真正地因此,您浪费了更多的时间(程序员每小时XX美元的成本比最终用户的成本)试图找到最好的
因此,如果你有成千上万条记录,我建议通过并行处理列表来提高效率,这种方法可以处理更多的记录,以节省线程开销的时间
IMHO如果记录计数大于100,则表示正在使用数据库。如果涉及数据库,则编写一个更新存储过程并暂停;我将很难编写一个一次性的程序来做一个特定的更新,这可以在所说的数据库中以一种更简单的方式完成 效率到底是什么 除非有成千上万的项目在做foreach、for或任何其他类型的循环操作,否则它们很可能只在千秒内显示差异。真正地因此,您浪费了更多的时间(程序员每小时XX美元的成本比最终用户的成本)试图找到最好的 因此,如果你有成千上万条记录,我建议通过并行处理列表来提高效率,这种方法可以处理更多的记录,以节省线程开销的时间
IMHO如果记录计数大于100,则表示正在使用数据库。如果涉及数据库,则编写一个更新存储过程并暂停;我将很难编写一个一次性的程序来做一个特定的更新,这可以在所说的数据库中以一种更简单的方式完成如果你想要快速查找,你可以考虑使用字典而不是列表。在您的情况下,它将是产品Id(我假设它是唯一的) 例如:
public class ProductRepository
{
Dictionary<int, Product> products = Product.GetProduct();
public void UpdateProducts(IEnumerable<Product> updatedProducts)
{
foreach(var productToUpdate in updatedProducts)
{
UpdateProduct(productToUpdate);
}
///update code here...
}
public void UpdateProduct(Product productToUpdate)
{
// get the product with ID 1234
if(products.ContainsKey(productToUpdate.ProductId))
{
var product = products[productToUpdate.ProductId];
///update code here...
product.ProductName = productToUpdate.ProductName;
}
else
{
//add code or throw exception if you want here.
products.Add(productToUpdate.ProductId, productToUpdate);
}
}
}
公共类产品存储库
{
Dictionary products=Product.GetProduct();
public void UpdateProducts(IEnumerable updatedProducts)
{
foreach(更新产品中的var productToUpdate)
{
更新产品(productToUpdate);
}
///在这里更新代码。。。
}
public void UpdateProduct(产品productToUpdate)
{
//获取ID为1234的产品
if(products.ContainsKey(productToUpdate.ProductId))
{
var product=产品[productToUpdate.ProductId];
///在这里更新代码。。。
product.ProductName=productToUpdate.ProductName;
}
其他的
{
//如果需要,请在此处添加代码或抛出异常。
products.Add(productToUpdate.ProductId,productToUpdate);
}
}
}
如果你想要快速查找,你可以考虑使用字典而不是列表。在您的情况下,它将是产品Id(我假设它是唯一的)
例如:
public class ProductRepository
{
Dictionary<int, Product> products = Product.GetProduct();
public void UpdateProducts(IEnumerable<Product> updatedProducts)
{
foreach(var productToUpdate in updatedProducts)
{
UpdateProduct(productToUpdate);
}
///update code here...
}
public void UpdateProduct(Product productToUpdate)
{
// get the product with ID 1234
if(products.ContainsKey(productToUpdate.ProductId))
{
var product = products[productToUpdate.ProductId];
///update code here...
product.ProductName = productToUpdate.ProductName;
}
else
{
//add code or throw exception if you want here.
products.Add(productToUpdate.ProductId, productToUpdate);
}
}
}
公共类产品存储库
{
Dictionary products=Product.GetProduct();
public void UpdateProducts(IEnumerable updatedProducts)
{
foreach(更新产品中的var productToUpdate)
{
更新产品(productToUpdate);
}
///在这里更新代码。。。
}
public void UpdateProduct(产品productToUpdate)
{
//获取ID为1234的产品
if(products.ContainsKey(productToUpdate.ProductId))
{
var product=产品[productToUpdate.ProductId];
///在这里更新代码。。。
product.ProductName=productToUpdate.ProductName;
}
其他的
{
//如果需要,请在此处添加代码或抛出异常。
products.Add(productToUpdate.ProductId,productToUpdate);
}
}
}
您的用例正在更新一个列表
,它可以包含数百万条记录,更新的记录可以是一个子列表,也可以只是一条记录
以下是模式:
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string Category { get; set; }
}
Product
是否包含主键,这意味着每个Product
对象都可以唯一标识,并且没有重复项,每个更新目标都有一个唯一的记录
如果是,则最好以字典
的形式排列列表
,这意味着对于IEnumerable
而言,每次更新都是一个O(1)
时间复杂度,这意味着所有更新都可以根据IEnumerable
的大小进行,我不希望它太大,尽管需要为不同的数据结构分配额外的内存,但是
public static class MyExtensions
{
// data - List<T>
// dataCount - Calculate once and pass to avoid accessing the property everytime
// Size of Partition, which can be function of number of processors
public static List<T>[] SplitList<T>(this List<T> data, int dataCount, int partitionSize)
{
int remainderData;
var fullPartition = Math.DivRem(dataCount, partitionSize, out remainderData);
var listArray = new List<T>[fullPartition];
var beginIndex = 0;
for (var partitionCounter = 0; partitionCounter < fullPartition; partitionCounter++)
{
if (partitionCounter == fullPartition - 1)
listArray[partitionCounter] = data.GetRange(beginIndex, partitionSize + remainderData);
else
listArray[partitionCounter] = data.GetRange(beginIndex, partitionSize);
beginIndex += partitionSize;
}
return listArray;
}
}
List<T>[] splitListArray = Fetch splitListArray;
// Process splitListArray
var finalList = splitListArray.SelectMany(obj => obj).ToList()