我经常发现自己需要switch打字.我知道这有很多关于Roslyn的讨论,但是由于我正在处理生产代码,我只是对已经存在的选项有一个标准的实践/性能问题:is和as.
鉴于课程,
abstract class Foo { }
class Bar : Foo { }
class Tada : Foo { }
Run Code Online (Sandbox Code Playgroud)
以下模式之间是否存在真正的差异?
Foo toUse = ...;
if (toUse != null)
{
Bar barInstance = toUse as Bar;
if (barInstance != null)
{
// use barInstance
}
else
{
Tada tadaInstance = toUse as Tada;
if (tadaInstance != null)
{
// use tadaInstance
}
else
{
// Check whatever other types there are, or throw a NotImplementedException …Run Code Online (Sandbox Code Playgroud) 通常,我使用String.sustring(int, int)方法来获取文本的某些部分.如果文字太大,那么我无法使用Integer类型变量访问某个位置,有什么方法可以解决这个问题?
例如:
我想用 String.substring(Long, Long)
我对C#很新,我无法将对象转换为List<T>.我一直收到错误"无法隐式类型转换Attachment到System.Collections.Generic.List<Attachment>.有很多关于我看过了类似的错误的职位,但我似乎无法找出什么我失踪.
我的核心对象如下:
public class Attachment
{
public Attachment() { }
...
}
Run Code Online (Sandbox Code Playgroud)
它在另一个类的构造函数中被调用,如下所示:
public class MyClass
{
...
public List<Attachment> attachments { get; set; };
...
public MyClass(JObject jobj)
{
...
//Attachments
if (jobj["attachments"] != null)
{
attachments = (Attachment)jobj.Value<Attachment>("attachments");
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误发生在代码的最后一行,我正在尝试将我的Attachment对象强制转换为List<attachments>.我理解这条消息的内容,但我尝试过的一切都行不通.
我必须有一个触发器来触发更新或删除操作。当更新某一列时,我的触发器工作正常。但是,当触发 DELETE 操作时,我需要不同的逻辑。我如何在一个触发器中同时拥有这两种逻辑?这是我到目前为止所拥有的:
ALTER TRIGGER [dbo].[Audit_Emp_Trigger]
ON [dbo].[EMPLOYEE]
AFTER UPDATE, DELETE
AS
BEGIN
--Only execute the trigger if the Dno field was updated or deleted
IF UPDATE(Dno)
BEGIN
--If the Audit_Emp_Record table does not exist already, we need to create it
IF OBJECT_ID('dbo.Audit_Emp_Record') IS NULL
BEGIN
--Table does not exist in database, so create table
CREATE TABLE Audit_Emp_Record
(
date_of_change smalldatetime,
old_Lname varchar (50),
new_Lname varchar (50),
old_ssn int,
new_ssn int,
old_dno int,
new_dno int
);
--Once table is created, …Run Code Online (Sandbox Code Playgroud) 我有一个包含string键和List<string>值的字典.我想为每个列表获取唯一值.
例如,这个输入数据:
{
"first": ["1", "2", "3", "1"],
"second": ["2", "3", "4", "3"]
}
Run Code Online (Sandbox Code Playgroud)
会回来这个:
{
"first": ["1", "2", "3"],
"second": ["2", "3", "4"]
}
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它不起作用:
var uniq = duplidictionary.GroupBy(x => x.Value)
.Select(y => y.First())
.ToDictionary( x => x.Key, y => y.Value);
Run Code Online (Sandbox Code Playgroud)
这似乎返回了原始字典的副本,而不是返回我预期的结果.这是一个DotNetFiddle,说明我的代码不起作用.
如何使用LINQ从给定输入获取此输出?
在MSDN上查看后,我发现我应该有以下代码的目标节点:
var listOffences = new LinkedList<string>();
listOffences.AddFirst("aaa");
listOffences.AddAfter("bbb"); // Requires target node
Run Code Online (Sandbox Code Playgroud)
我不知道如何获得第一个节点所需的信息,有人能指出我正确的方向吗?
c# ×4
.net ×1
casting ×1
dictionary ×1
generics ×1
java ×1
linked-list ×1
linq ×1
long-integer ×1
performance ×1
sql-server ×1
string ×1
substring ×1
t-sql ×1