草庐IT

kernel-lt

全部标签

c# - Expression<Func<T,bool>> 声明是什么意思?

有人可以用表达表达式的含义以及如何调用它的方式来解释以下声明吗?voidDelete(Expression>expression)whereT:class,new();我是这样读的:删除T类型的对象,通过传入一个参数为T类型对象的lambda表达式返回bool.还有,你能不能换Funcexpression和Predicateexpression 最佳答案 此方法可能是集合类型的成员,是吗?“谓词”是对“这个东西是那个集合的成员吗?”这个问题说"is"或“否”的任何设备。因此,集合“整数偶数正整数”的谓词将是x=>x>0&&x%2==

c# - Fluent Assertions 能否对 IEnumerable<string> 使用不区分字符串的比较?

我有一对列表,我正在尝试使用FluentAssertions进行比较。我可以很容易地编写比较代码,但我想使用FluentAssertions,这样我就可以获得在测试失败消息中显示的原因。到目前为止,我所看到的一切似乎都在使用默认的Object.Equals比较,它区分大小写。我似乎无法将IComparer传递给Equal或Contains方法,还有其他方法吗?[TestMethod()]publicvoidfoo(){varactual=newList{"ONE","TWO","THREE","FOUR"};varexpected=newList{"One","Two","Three"

c# - List<T> 是链表吗?

是System.Collections.Generic.List一种linkedlist(不是LinkedList类)?Alinkedlistisadatastructureconsistingofagroupofnodeswhichtogetherrepresentasequence.Underthesimplestform,eachnodeiscomposedofadatumandareference(inotherwords,alink)tothenextnodeinthesequence.Alinkedlistwhosenodescontaintwofields:aninteg

c# - IEnumerable<T> 线程安全?

我有一个填充List的主线程.此外,我创建了一个将在不同线程上执行的对象链,需要访问列表。原始列表生成后将永远不会被写入。我的想法是将列表传递为IEnumerable到在其他线程上执行的对象,主要是为了不允许那些实现这些对象的人错误地写入列表。换句话说,如果保证不写入原始列表,多线程使用.Where是否安全?或foreach在IEnumerable上?如果原始集合从未更改,我不确定迭代器本身是否是线程安全的。 最佳答案 IEnumerable无法修改。那么什么可以是非线程安全的呢?(如果您不修改实际的List)。对于非线程安全,您需

c# - 如何将 XML 文件读入 List<>?

我有一个List我将其写入XML文件。现在我正在尝试读取同一个文件并将其写回List.有没有办法做到这一点? 最佳答案 我认为最简单的方法是使用XmlSerializer:XmlSerializerserializer=newXmlSerializer(typeof(List));using(FileStreamstream=File.OpenWrite("filename")){Listlist=newList();serializer.Serialize(stream,list);}using(FileStreamstream=

c# - 验证错误 : The value 'on' is not valid for <<property name>>

在我的项目中,我有一个模型,您可以在这里看到我模型的一部分:publicclassCheckoutModel{publicboolOtherPlace{get;set;}[RequiredIf("OtherPlace",true,ErrorMessage="")]publicstringOtherPlaceFullName{get;set;}[RequiredIf("OtherPlace",true,ErrorMessage="")]publicintOtherPlaceProvinceId{get;set;}[RequiredIf("OtherPlace",true,ErrorMes

c# - 从 linq 查询返回 Dictionary<string, string>

我有一个表,其中2列定义为varchar(50):Column1和Column2。我想返回的字典其中每一行都在字典中,其中Column1是键,Column2是值。这是我的:publicDictionaryLoadAllTheDataFromDB(){using(MyDCTheDC=newMyDC()){return(fromcinTheTableselectnewDictionary(){//stuckhere}).FirstOrDefault();}}如何让字典充满? 最佳答案 试试这个:vardict=TheTable.Sele

c# - 如何将 System.Linq.Enumerable.WhereListIterator<int> 转换为 List<int>?

在下面的示例中,我如何轻松转换eventScores至List这样我就可以将它用作prettyPrint的参数?Console.WriteLine("ExampleofLINQ'sWhere:");Listscores=newList{1,2,3,4,5,6,7,8};varevenScores=scores.Where(i=>i%2==0);Action,string>prettyPrint=(list,title)=>{Console.WriteLine("***{0}***",title);list.ForEach(i=>Console.WriteLine(i));};score

c# - Task.Run 和 Func<>

如何运行一个返回值并接受参数的任务?我看到有一个重载方法Task.Run(Func)但是我怎样才能在那里传递参数呢? 最佳答案 Func不带参数。通常,您会使用lambda表达式捕获参数。例如:publicvoidDoSomething(stringtext){Tasktask=Task.Run(()=>text.Length);...}在这里text是一个捕获的变量...所以即使你只是创建一个Func,它使用方法参数。 关于c#-Task.Run和Func,我们在StackOverfl

c# - list<T> 字段的平均计数

我有A的列表,我想计算它的字段a的平均值。最好的方法是什么?classA{inta;intb;}voidf(){varL=newList();for(inti=0;i 最佳答案 Enumerable.Average有一个需要Func的重载作为论点。usingSystem.Linq;list.Average(item=>item.a); 关于c#-list字段的平均计数,我们在StackOverflow上找到一个类似的问题: https://stackoverf