我有一个表示对象的简单类.它有5个属性(日期,2位小数,一个整数和一个字符串).我有一个派生类,派生自CollectionBase,它是一个容器类,用于从我的第一个类中保存多个对象.
我的问题是,我想删除重复的对象(例如,具有相同日期,相同小数,相同整数和相同字符串的对象).是否有可以编写的LINQ查询来查找和删除重复项?或者至少找到它们?
除了非人类可读代码之外还有另一个原因是不对函数中的每个变量使用var吗?我的意思是性能命中不是使用int,SqlCommand,string而是使用var而不是?
我可以看到很多非常相似的线程,但似乎没有什么能给我一个应该是非常基本的解决方案.
从我的winforms应用程序中,我需要关闭一个word文档的运行实例(从应用程序本身打开).当我从应用程序中打开word文档时,我会在列表中跟踪它.现在我该如何关闭同一个doc?
这是我尝试过的:
private bool CloseWord(string osPath) //here I pass the fully qualified path of the file
{
try
{
Word.Application app = (Word.Application)Marshal.GetActiveObject("Word.Application");
if (app == null)
return true;
foreach (Word.Document d in app.Documents)
{
if (d.FullName.ToLower() == osPath.ToLower())
{
d.What? //How to close here?
return true;
}
}
return true;
}
catch
{
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
我得到了很多文档对象的方法,但只有一个.Close()关闭哪个有这样的参数:ref object SaveChanges, ref object OriginalFormat, ref object RouteDocument我不明白.
什么是理想的方式?谢谢..
编辑:
我无法关闭整个Word应用程序(WinWord),因为用户可能打开了其他word文件.
我需要终止单词实例(类似的东西Process.Kill())而不提示用户保存或不保存等.
我正在寻找将整个方法转换为表达式树的能力.写出来会很糟糕.:)
所以(琐碎的例子)给出以下文字:
public static int Add(int a, int b)
{
return a + b;
}
Run Code Online (Sandbox Code Playgroud)
我想获得一个代表这个的内存中对象,或者下面的文本:
ParameterExpression a = Expression.Parameter(typeof(int), "a");
ParameterExpression b = Expression.Parameter(typeof(int), "b");
var expectedExpression = Expression.Lambda<Func<int, int, int>>(
Expression.Add(a,b),
a,
b
);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?有没有人可以与Roslyn做点什么呢?
编辑:澄清:我想把任何C#方法(例如,上面的方法)作为文本,并产生一个结果表达式.基本上我正在寻找将任何给定的C#方法编译到表达式树中.
我是否正确地认为任何不从System.ValueType继承的对象因此必须是引用类型?
我一直无法找到任何确凿的文档来备份这个概念.
也许我错过了一些微不足道的东西.我有几个,List<T>我需要一个大的列表,这是所有其他列表的联合.但我确实希望他们在那个大名单中引用而不仅仅是值/副本(不像我通常在SO上找到的许多问题).
比如我有这个,
List<string> list1 = new List<string> { "a", "b", "c" };
List<string> list2 = new List<string> { "1", "2", "3" };
var unionList = GetThatList(list1, list2);
Run Code Online (Sandbox Code Playgroud)
假设我得到了我想要的列表unionList,那么这应该发生:
unionList.Remove("a"); => list1.Remove("a");
unionList.Remove("1"); => list2.Remove("1");
//in other words
//
//unionList.Count = 4;
//list1.Count = 2;
//list2.Count = 2;
Run Code Online (Sandbox Code Playgroud)
为了说清楚,这通常发生在
unionList = list1; //got the reference copy.
Run Code Online (Sandbox Code Playgroud)
但是如何list2添加第二个列表unionList?
我尝试了Add,AddRange但他们显然克隆而不是复制.
unionList = list1;
unionList.AddRange(list2); //-- error, clones, not copies here.
Run Code Online (Sandbox Code Playgroud)
和 …
我做了一些基准测试时遇到了这个问题.
bool b;
MyStruct s;
for (int i = 0; i < 10000000; i++)
{
b = (object)s == null;
}
Run Code Online (Sandbox Code Playgroud)
调试:200毫秒
发布:5毫秒
bool b;
MyStruct? s = null;
for (int i = 0; i < 10000000; i++)
{
b = (object)s == null;
}
Run Code Online (Sandbox Code Playgroud)
调试:800毫秒
发布:800毫秒
我可以理解这个结果,因为转换可为空的结构给object了我一个盒装类型的结构.但是为什么不进行转换struct s以object进行空比较(如在第一种方法中)导致相同的性能?编译器是否正在优化调用以返回,false因为结构不能为null?
考虑以下枚举类
public enum ClassA {
CHECK1("X", 0),
CHECK2("Y", 2),
CHECK3("Z", 1);
private final String id;
private final String cdValue;
private ClsA(String id, String cdValue) {
this.id = id;
this.cdValue = cdValue;
}
private String getId() {
return id;
}
private String getCdValue() {
return cdValue ;
}
private static final List<String> cdValues = new ArrayList<String>();
static {
for (ClassA clsA : ClassA.values()) {
cdValues.add(clsA.getCdValue());
}
}
public boolean isCdValue(String cdValue)
{
if clsValues.contains(cdValue)
return true;
else return false;
}
} …Run Code Online (Sandbox Code Playgroud) enum Gender { Male, Female }
var k = new[] { Gender.Male }.Cast<int>().ToList().Cast<int?>().ToList(); //alright
var p = new[] { Gender.Male }.Cast<int>().Cast<int?>().ToList(); //InvalidCastException
Run Code Online (Sandbox Code Playgroud)
第二种情况的原因是什么?我知道我不能投了盒装enum,以int?直接,但我做了两个阶段铸造,即Cast<int>.Cast<int?>应工作.
编辑:
考虑到以下工作,这是令人惊讶的:
object o = Gender.Male;
int i = (int)o; // so here the cast is not to an entirely different type, which works
Run Code Online (Sandbox Code Playgroud) 我应该维护
1.)一个SortedDictionary(double,struct)
2.)或者只是一个普通的Dictionary(double,struct)加一个SortedSet(double)?
我只想要快速插入.我不关心检索,因为我不会做很多查找.我需要排序自然因为,我所做的唯一查找将是最大双倍或几个最大双倍.
我觉得时间表现明智 - 两者都是一样的,SortedSet<double>只做额外的工作.你们能证实吗?
我不知道的部分是维持排序,SortedDictionary仅仅是键(双打),还是键和值的移动.在后一种情况下2.)将胜过1.),不是吗?
此外,尚不清楚SortedDictionary内部如何实施.Sortedset是红黑树,是一个经过验证的表演者.
c# ×7
.net ×4
casting ×2
enums ×2
performance ×2
boxing ×1
c#-2.0 ×1
c#-4.0 ×1
clone ×1
collections ×1
copy ×1
duplicates ×1
generic-list ×1
java ×1
linq ×1
nullable ×1
roslyn ×1
sortedset ×1
static ×1
value-type ×1
var ×1
vb.net ×1