本文介绍了动态加载数组中的数据表,而不是手动编写列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对于药物 - 药物相互作用软件, ,以下代码包含将出现在自动完成列表中的药物/药物数组。 < System.Web.Services.WebMethodAttribute(),System.Web.Script.Services.ScriptMethodAttribute()> 公共 共享 功能 GetCompletionList ( ByVal prefixText 作为 字符串, ByVal count 作为 整数, ByVal contextKey 作为 字符串)作为 字符串() ' 创建药物数组 Dim drugs()作为 字符串 = { Methotrexate , 甘露醇, 别嘌呤醇, Aspirin, 维生素A, 维生素C, 维生素D, 维生素E, 阿莫西林, Mercaptopurine, Diclofenac } ' 返回匹配药物 返回(从m 在药物 Wh ere m.StartsWith(prefixText,StringComparison.CurrentCultureIgnoreCase) 选择 m).Take(count).ToArray() 结束 功能 我需要一种方法在这个阵列中动态加载5,000药物的数据表,而不是手动编写药物清单 i使用此代码,但doenot似乎有所帮助:( Dim druggv As 新 GridView druggv.DataSource = drugsds druggv.DataBind() Dim i 作为 整数 = druggv.Rows.Count 对于 x = 0 要 i - 1 Dim singleItem 作为 String = druggv.Rows(x).Cells( 1 )。Text.ToString Dim list As String = list + , + singleItem 下一步 解决方案 最简单的方法是使用一个文本文件,其中每种药物都在一行: 甲氨喋呤甘露醇别嘌呤醇阿司匹林维生素A 维生素C 维生素D 维生素E 阿莫西林巯基嘌呤 Diclofenac 然后你可以使用File.ReadAllLines将它们全部加载到一个数组中: Dim drugs() As String = File.ReadAllLines( D:\Temp \DrugsList.txt)然后继续你好。 从那时起,有大量的解决方案:XML文件,CSV文件,数据库等。 如果您打算使用linq查找以一组特定字符开头的药物,那么你不需要首先拥有一个字符串数组。只需从数据表中拉出它们即可。如果您需要帮助,我建议您阅读 LINQ简介第3部分 [ ^ ]。 关于你试过的代码......如果要迭代数据表,有一种比你设置的For循环更简单的方法。 ..你可以使用 For Each Statement [ ^ ]。它只是更好,因为你不必弄乱弄清楚有多少行和类似的东西。 以及你的代码不起作用与范围有关。您在For语句中声明变量。在For循环中声明的任何变量都将被丢弃,当它到达Next语句并转到下一行时将无法访问。如果你想保留你在循环中得到的数据,你需要在循环外声明要包含它的变量。 I 我也不确定你在使用GridView做什么......我以为你想要一个字符串数组?也许你应该解释一下,如果我所说的并不是你想要完成的事情。 我希望这会有所帮助。 for a drug-drug interaction software, the following code contains an array for drugs/ medications that will appear in the autocomplete list.<System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()> Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal contextKey As String) As String() ' Create array of Drugs Dim drugs() As String = {"Methotrexate", "Mannitol", "Allopurinol", "Aspirin", "Vitamin A", "Vitamin C", "Vitamin D", "Vitamin E", "Amoxicillin", "Mercaptopurine", "Diclofenac"} ' Return matching drugs Return ( From m In drugs Where m.StartsWith(prefixText, StringComparison.CurrentCultureIgnoreCase) Select m).Take(count).ToArray() End Functioni need a way to dynamically load a datatable of 5,000 medication in this array instead of writing the medication list manuallyi used this code but doenot seem to help :(Dim druggv As New GridView druggv.DataSource = drugsds druggv.DataBind() Dim i As Integer = druggv.Rows.Count For x = 0 To i - 1 Dim singleItem As String = druggv.Rows(x).Cells(1).Text.ToString Dim list As String = list + ", " + singleItem Next 解决方案 The simplest way would be to use a text file, where each drug is on a single line:MethotrexateMannitolAllopurinolAspirinVitamin AVitamin CVitamin DVitamin EAmoxicillinMercaptopurineDiclofenacYou could then load them all into an array using File.ReadAllLines:Dim drugs() As String = File.ReadAllLines("D:\Temp\DrugsList.txt")And then continue ar you are.From there on up, there are a huge number of solutions: XML files, CSV files, databases, and so forth.If you are going to use linq to find the drugs that start with a certain set of characters, then you do not NEED to have a string array hold the names in the first place. Just pull them right from the datatable. If you need help with that, I suggest reading LINQ Introduction Part 1 Of 3[^].In regards to the code you tried...if you are going to iterate through a datatable, there is an easier way than the For Loop that you have setup...you can use a For Each Statement[^]. It''s just a lot nicer because you don''t have to mess with figuring out how many rows there are and stuff like that.And the reason that your code isn''t working has to do with scope. You are declaring variables inside your For statement. Any variable that is declare in the For loop will be thrown away and not accessible when it hits the Next statment and goes to the next row. If you want to keep data that you''ve gotten from inside a loop, you need to declare the variable that is going to contain it outside the loop.I''m also not sure what you were doing with the GridView...I thought you wanted a string array? Perhaps you should explain more if what I''ve said isn''t really what you are trying to accomplish.I hope this helps. 这篇关于动态加载数组中的数据表,而不是手动编写列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-05 00:12