简体   繁体   English

这是根据数据库值在C#高亮列表框中将字符串传递给List的更好方法

[英]Which is better way of passing string to List in C# highlighting listbox based on database values

I would appreciate if someone can tell me what is the better way of defining list and passing a string to it 如果有人能告诉我定义list并将字符串传递给它的更好方法,我将不胜感激

I am not sure which one to use or which one is better from performance point of view 从性能的角度来看,我不确定要使用哪个还是更好的一个

var selection = "28,2,10,30,100,51";
List<string> categories = selection.Split(',').ToList();
List<string> categories = new List<string>(selection.Split(',')); 

I actually want to highlight the Listbox items based on database selection 我实际上想根据数据库选择突出显示列表框项目

after creating my list i loop through them & use following code to highlight the selection in the multi-selection list-box in asp.net 创建我的列表后,我遍历它们并使用以下代码在asp.net的多选列表框中突出显示该选择

foreach (ListItem item in lstCatID.Items)
{
    if (categories.Contains(item.Value))
        item.Selected = true;
}

Is the the best way to do it or it can be done in any other way to enhance performance. 是执行此操作的最佳方法,也可以通过其他任何方法来提高性能。

ToList internally calls the List constructor taking an argument of type IEnumerable so for both of your cases it would be same. ToList内部调用List构造函数,并接受IEnumerable类型的参数,因此在两种情况下都相同。

You should see: Reimplementing LINQ to Objects: Part 20 - ToList (Jon Skeet) 您应该看到: 重新实现对象的LINQ:第20部分-ToList(乔恩·斯凯特)

You may be wondering why we even need ToList, if we could just create a list by calling the constructor directly. 您可能想知道为什么我们甚至需要ToList,如果我们可以直接通过调用构造函数来创建列表。 The difference is that in order to call a constructor, you need to specify the element type as the type argument. 区别在于,要调用构造函数,您需要将元素类型指定为type参数。

It would be better for you if you can time them both using Stopwtach to see the difference, Also first make sure your code works and then worry about the performance. 如果您可以同时使用Stopwtach来计时它们,以查看两者之间的差异,那么这对您会更好。另外,请首先确保您的代码正常工作,然后再考虑性能。 Usually performance optmization for this kind of task results in negligible improvements. 通常,针对此类任务的性能优化导致的改进可忽略不计。

If you are just using it to read value try using IEnumerable<string> instead if List<string> which is lighter and restrictive than list. 如果您只是使用它来读取值,请尝试使用IEnumerable<string>而不是List<string>比list更轻和更严格的列表。 When you use IEnumerable, you give the compiler a chance to defer work until later, possibly optimizing along the way. 使用IEnumerable时,可以使编译器有机会将工作推迟到以后,可能会一直进行优化。 SO while using Linq expressions like contains that you are using here IEnumerable probably is the best bet. 因此,在使用Linq表达式(例如contains ,最好在IEnumerable上使用。 Apart from this many a times during desin pattern when you want to transfer list of items between 2 objects again IEnumerable is a best bet since it is more restrictive. 除了在设计模式中要多次在2个对象之间转移项目列表之外,IEnumerable还是最好的选择,因为它的限制性更强。

var selection = "28,2,10,30,100,51";          
IEnumerable<string> categories = selection.Split(',');

foreach (ListItem item in lstCatID.Items)
{
  if (categories.Contains(item.Value))
     item.Selected = true;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM