我有一个定义如下的接口:
public interface TestInterface{
int id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
两个实现该接口的Linq-to-SQL类:
public class tblTestA : TestInterface{
public int id { get; set; }
}
public class tblTestB : TestInterface{
public int id { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有来自tblTestA和tblTestB的数据库记录填充的IEnumerable列表a和b
IEnumerable<tblTestA> a = db.tblTestAs.AsEnumerable();
IEnumerable<tblTestB> b = db.tblTestBs.AsEnumerable();
Run Code Online (Sandbox Code Playgroud)
但是,不允许以下内容:
List<TestInterface> list = new List<TestInterface>();
list.AddRange(a);
list.AddRange(b);
Run Code Online (Sandbox Code Playgroud)
我必须做如下:
foreach(tblTestA item in a)
list.Add(item)
foreach(tblTestB item in b)
list.Add(item)
Run Code Online (Sandbox Code Playgroud)
有什么我做错了吗?谢谢你的帮助
使用控制台应用程序时,Console.ReadLine()会存储在a 处输入的所有内容的历史记录.在控制台提示输入内容时,按向上/向下光标将滚动浏览此历史记录(按F7可查看整个历史记录).
使用C#,是否有办法禁用此行为或清除已输入的内容的历史记录?
澄清Console.Clear()一下,不清楚历史,只有屏幕缓冲区.我想清除命令历史记录.
编辑:尝试了几种建议的方法,以及我自己设计的一些方法,最好的方法是ho1建议的方法.它并不理想,因为它会带来另一个控制台窗口,但它确实清除了历史记录.
我想编写一个读取文件的函数,并计算每个单词出现的次数.假设处理文件读取并生成表示文件中每一行的字符串列表,我需要一个函数来计算每个单词的出现次数.首先,是使用Dictionary<string,int>最好的方法?关键是单词,值是该单词的出现次数.
我编写了这个函数,它遍历每一行和一行中的每个单词并构建一个字典:
static IDictionary<string, int> CountWords(IEnumerable<string> lines)
var dict = new Dictionary<string, int>();
foreach (string line in lines)
{
string[] words = line.Split(' ');
foreach (string word in words)
{
if (dict.ContainsKey(word))
dict[word]++;
else
dict.Add(word, 1);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想以某种方式编写这个函数..功能上,使用LINQ(因为LINQ很有趣,我正在努力提高我的函数编程技能:D)我设法得出这个表达式,但我不确定是否是在功能上做到这一点的最佳方式:
static IDictionary<string, int> CountWords2(IEnumerable<string> lines)
{
return lines
.SelectMany(line => line.Split(' '))
.Aggregate(new Dictionary<string, int>(),
(dict, word) =>
{
if (dict.ContainsKey(word))
dict[word]++;
else
dict.Add(word, 1);
return dict;
});
}
Run Code Online (Sandbox Code Playgroud)
因此,虽然我有两个有效的解决方案,但我也有兴趣了解这个问题的最佳方法.有兴趣了解LINQ和FP的人吗?
我想在目标C中询问有关NSString*的问题.我可以检查NSString*对象的最后一个字符吗?
例:
NSString* data = @"abcde,";
if(data is end with ',') // I don't know this part
// do sth
Run Code Online (Sandbox Code Playgroud)
非常感谢你.
我尝试重新定义\ FancyVerbLine有一个\ large,但这没有帮助.还有另一种方法吗?
什么是bash命令,用于检测所有当前连接的USB设备并获取与USB设备对应的/ dev/tty ...文件.
单击按钮图像时如何在按钮中设置图像显示该按钮run.backgroundColor = [UIColor redColor]我试试这个但它不起作用,
我想用一些颜色(图像)徽章文件和文件夹,任何想法如何实现我尝试使用图标服务,它适用于文件,但它不适用于文件夹.
我看到这种行为工作Dropbox(10.4,10.5和10.6) - 任何想法如何做到这一点?
以下对我来说非常接近,但它没有按预期工作. http://www.cimgf.com/2008/06/16/cocoa-tutorial-custom-folder-icons/
除了图标服务之外还有其他解决方案吗?
我感谢任何帮助.
我有这样的字符串:
string val = 555*324-000
Run Code Online (Sandbox Code Playgroud)
现在,我需要删除*和 - 字符,所以我使用该代码(基于MSDN)
char[] CharsToDelete = {'*', '(', ')', '-', '[', ']', '{', '}' };
string result = val.Trim(CharsToDelete);
Run Code Online (Sandbox Code Playgroud)
但字符串保持不变.什么原因 ?