我有一个函数叫做getWords(int wrNum)wich从数据库中使用实体框架返回wrNum数字随机数,当wrNum为300时,这个函数需要花费10秒的时间.我不明白为什么需要这么多时间.请帮助我的代码出了什么问题?
public List<string> getWords(int wrNum)
{
IDVLTest d3 = new DVLTest();
for (int i = 0; i < wrNum;i++ )
{
string word = d3.getRandomWord().Text;
Words.Add(new WordView { Word = word, Status = WordStatus.Right });
}
return Words.Select(w=>w.Word).ToList();
}
public class DVLTest:IDVLTest
{
...
public Word getRandomWord()
{
Random r = new Random();
int i = r.Next(DVL_Entitie.Words.Count());
return DVL_Entitie.Words.ToList().ElementAt(i);
}
...
}
Run Code Online (Sandbox Code Playgroud) 我在C#中有这种代码,如果有更好的计算Name属性的解决方案,我很有意思:
dictItem.valueElement.Select(li => new ItemWithNameAndUrl {
Name = StringParser.getBeforeString("(", li.InnerText) == string.Empty ?
li.InnerText : StringParser.getBeforeString("(", li.InnerText), //don't like it
Url = li.Element("a").Attributes["href"].Value
}).ToList());
Run Code Online (Sandbox Code Playgroud)
有没有办法不StringParser.getBeforeString为Name属性调用方法两次?
我想要这样简单的功能:
string getStringWithCharAndLength(char ch, int l)
{
//Some easy code
//return ch+ch+...+ch(string with Length l)
}
Run Code Online (Sandbox Code Playgroud)
例:
string str = getStringWithCharAndLength('k',5);<br>
Console.WriteLine(str);// "kkkkk"
Run Code Online (Sandbox Code Playgroud)
注意:不是这种解决方案:
string getStringWithCharAndLength(char ch, int l)
{
string str="";
for(int i=0;i<l;i++)
str+=ch;
return str;
}
Run Code Online (Sandbox Code Playgroud)