从功能上讲,C#和VB.Net中的lambda表达式之间是否存在差异(除了语法之外)?
编辑:跟进CraigTP的回答:任何对.Net 4情况的引用?
编辑:我问,因为我习惯了C#,但是对于下一个项目,客户要求VB.Net.我们不是先验者.我们意识到大多数语言结构都支持两种语言.但是,我们特别喜欢C#实现lambda表达式的方式.我们希望概述与VB.Net的差异
编辑:接受CraigTP的答案,指出我目前认为最重要的区别.
总结一下:VB.Net 9不支持lambda表达式中的多行语句,而lambda必须始终返回一个值.这两个问题都在VB.Net 10中得到解决
使用xUnit Assert.Throws,我偶然发现(对我来说)难以解释重载解析问题.在xUnit中,此方法已标记为已过时:
[Obsolete("You must call Assert.ThrowsAsync<T> (and await the result) " +
"when testing async code.", true)]
public static T Throws<T>(Func<Task> testCode) where T : Exception
{ throw new NotImplementedException(); }
Run Code Online (Sandbox Code Playgroud)
问题是,为什么简单地抛出异常的内联语句lambda(或表达式)会解析此重载(因此不能编译)?
using System;
using Xunit;
class Program
{
static void Main(string[] args)
{
// this compiles (of course)
// resolves into the overload accepting an `Action`
Assert.Throws<Exception>(() => ThrowException());
// line below gives error CS0619 'Assert.Throws<T>(Func<Task>)' is obsolete:
// 'You must call Assert.ThrowsAsync<T> (and await the …Run Code Online (Sandbox Code Playgroud) 假设这是我的成员类
class Member
{
public string CategoryId { get; set; }
public string MemberName { get; set; }
public int Distance { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
而且,这是列表.
var list = new List<Member>();
list.Add(new { CategoryId = "01", MemberName="andy" Distance=3});
list.Add(new { CategoryId = "02", MemberName="john" Distance=5});
list.Add(new { CategoryId = "01", MemberName="mathew" Distance=7});
list.Add(new { CategoryId = "03", MemberName="bakara" Distance=2});
Run Code Online (Sandbox Code Playgroud)
任何人都可以建议逻辑/ linq查询,以使List具有最短距离的不同/唯一categoryID.
本output应该是:
list.Add(new { CategoryId = "01", MemberName="andy" Distance=3});
list.Add(new { CategoryId = "02", MemberName="john" Distance=5});
list.Add(new …Run Code Online (Sandbox Code Playgroud) 我刚刚设法与svn陷入了一种奇怪的境地.就工作副本而言,我的一个文件似乎"丢失"了.当我在服务器上查看或在另一个位置对包含文件夹进行新的检查时,文件就在那里,但在这个特定的工作副本中,它似乎"在空间中丢失".
svn状态报告没有更改(所以甚至没有丢失的文件)
svn update什么都不做
我甚至尝试将文件重新导出到我的工作副本,但没效果.
如果可能的话,我想避免不得不对整个工作副本进行新的检查.
[编辑:我在下面留下了原始问题,有一些更多的上下文和代码来重现问题.下面的简短版本包含问题的本质]
简短版本:下面的查询抛出一个System.NotSupportedException:"无法1' to type 'System.Linq.IQueryable强制转换类型'System.Linq.IOrderedQueryable 1'.LINQ to Entities仅支持转换实体数据模型基元类型." 仅在VB.Net版本中引发异常.转换为C#时,不会引发异常.
Dim doesThisCrash = From outerOrder In orders
Where outerOrder.ProductId =
(From p In products Join o In orders On p.Id Equals o.ProductId
Order By p.Id
Select p.Id).FirstOrDefault()
Select outerOrder
doesThisCrash.ToList()
Run Code Online (Sandbox Code Playgroud)
因此,为了使它崩溃,似乎我们需要一个子查询,其中原始ObjectSet(订单)与另一个ObjectSet(产品)连接,并且有序.仅使用订单或设置的产品时,不会发生崩溃.当省略Order By时,也没有崩溃.
我倾向于认为这是一个(VB.Net)编译器错误,除非有一些明显我在这里忽略的东西......
现在我的问题仍然存在:
[/编辑]
可选,更长版本(原始问题):
我的域看起来非常不同,但我将问题转换为更简单的版本,具有以下实体(注意:我实际上使用.edmx设计器定义了这些,因此这是一个简化版本):
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime DateCreated { get; set; }
}
public class Order
{ …Run Code Online (Sandbox Code Playgroud) 我需要在单元测试中从appsettings部分(在app.config中定义)中读取设置.我们在这个项目中使用mstest.
说这是app.config:
<configuration>
<appSettings>
<add key="MyAppSetting" value="MyAppSettingValue"/>
</appSettings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这是相应的测试,它通过了这个设置:
[TestClass]
public class ConfigurationTests
{
[TestMethod]
public void can_read_appsettings()
{
string value = ConfigurationManager.AppSettings.Get("MyAppSetting");
Assert.AreEqual("MyAppSettingValue", value);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当我尝试将appSettings部分移动到custom.config文件时,此测试失败.
这就是我的app.config文件现在的样子:
<configuration>
<appSettings file='Custom.config' />
</configuration>
Run Code Online (Sandbox Code Playgroud)
我将Custom.config文件添加到我的项目中(使用构建操作'始终复制'):
<appSettings>
<add key="MyAppSetting" value="MyAppSettingValue"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
在控制台应用程序中执行相同操作时,这是有效的.有没有办法在单元测试装配中使这个工作?
假设我有以下IronPython脚本:
def Calculate(input):
return input * 1.21
Run Code Online (Sandbox Code Playgroud)
当使用小数从C#调用时,此函数返回一个double:
var python = Python.CreateRuntime();
dynamic engine = python.UseFile("mypythonscript.py")
decimal input = 100m; // input is of type Decimal
// next line throws RuntimeBinderException:
// "cannot implicitly convert double to decimal"
decimal result = engine.Calculate(input);
Run Code Online (Sandbox Code Playgroud)
我似乎有两个选择:
首先,我可以在C#侧投射:看起来像是一个禁区,因为我可能会失去精确度.
decimal result = (decimal)engine.Calculate(input);
Run Code Online (Sandbox Code Playgroud)
第二个选项是在python脚本中使用System.Decimal:工作,但稍微污染脚本...
from System import *
def CalculateVAT(amount):
return amount * Decimal(1.21)
Run Code Online (Sandbox Code Playgroud)
是否有一个简写符号DLR,数字1.21应该被解释为十进制,就像我在C#中使用'1.21m'符号一样?或者有没有其他方法来强制使用十进制而不是double?
当我尝试使用它时,我刚刚添加了对我们自己的Dll之一的引用我无法因为新引用的dll缺失了.
这是怎么回事?添加引用时,是否应将其引用的所有链接dll添加到?(除了GAC中的那些)......?
场景:我正在尝试使用 Roslyn 将一组 C# 源代码片段合并为一个代码片段。
问题:解析带有前导注释的不同类时,第一个类(SomeClass在示例中)上方的注释不会保留。对于第二个类 ( AnotherClass),注释被保留...
下面的代码演示了这个问题。在控制台程序中使用它(并安装 Microsoft.CodeAnalysis.CSharp nuget 包)
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Collections.Generic;
using System.Diagnostics;
using System;
namespace roslyn01
{
class Program
{
static void Main(string[] args)
{
var input = new[]
{
"// some comment\r\nclass SomeClass {}",
"// another comment\r\nclass AnotherClass {}",
};
// parse all input and collect members
var members = new List<MemberDeclarationSyntax>();
foreach (var item in input)
{
var syntaxTree = CSharpSyntaxTree.ParseText(item);
var compilationUnit = (CompilationUnitSyntax)syntaxTree.GetRoot(); …Run Code Online (Sandbox Code Playgroud) 我不是程序员,但我试图通过给他们一些指导来帮助他们。我们不再拥有任何有关 msmq 的内部专业知识。我们正在尝试使用它来将一些功能与调度应用程序集成。
调度应用程序通过使用自定义构建的 dll 进行网络调用来启动作业。dll 调用 weburl。Web 应用程序将运行其任务并向网站发送有关其执行的任务的更新。网站将消息写入队列。调用该站点的 dll 正在监视队列中是否有带有分配给该作业的标签的消息。当它收到最终状态消息时,它会关闭。
我们每隔几个小时就会收到以下消息。我们每小时运行近 100 个使用此方法的作业。在底部列出的代码中,jobid 对应于消息队列中消息的标签。每个作业在开始时都会发出一个 jobid,并将使用它作为发送到该作业的 msmq 的每条消息的标签。
System.Messaging.MessageQueueException (0x80004005): Message that the cursor is currently pointing to has been removed from the queue by another process or by another call to Receive without the use of this cursor.
at System.Messaging.MessageQueue.ReceiveCurrent(TimeSpan timeout, Int32 action, CursorHandle cursor, MessagePropertyFilter filter, MessageQueueTransaction internalTransaction, MessageQueueTransactionType transactionType)
at System.Messaging.MessageEnumerator.get_Current()
Run Code Online (Sandbox Code Playgroud)
这是它的代码。
while ( running )
{
// System.Console.WriteLine( "Begin Peek" );
messageQueue.Peek();
//System.Console.WriteLine( "End Peek" ); …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×2
vb.net ×2
.net-4.0 ×1
app-config ×1
assemblies ×1
c#-4.0 ×1
ironpython ×1
lambda ×1
linq ×1
literals ×1
msmq ×1
mstest ×1
reference ×1
roslyn ×1
svn ×1
tortoisesvn ×1
types ×1
visualsvn ×1
xunit ×1