Tag: 元组

如何在Dapper中使用generics和Nullable 类型进行实现?

我有这个电话: public IObservable Subscribe(object topic, string service, string connectionString, string query) { try { this.connection.ConnectionString = connectionString; this.connection.Open(); this.connection.Query(query, new { transactionid = topic }).ToObservable().Subscribe(message => this.subject.OnNext(message)); return this.subject; } catch (Exception e) { this.subject.OnError(e); return this.subject; } finally { this.subject.OnCompleted(); this.connection.Close(); } } 这是我的查询: with IDS as (select L1_ID, L2_ID, L1_VALUE, L2_VALUE from MYDB […]

为什么在C#中使用1元组,Tuple ?

我没有看到在C#中使用1元组。 为什么程序员会使用它们? 元组类 我看过如下声明: Tuple state; 但我想知道是否还有其他用途。

绑定到元组列表

我有一个元组列表配对两个数据……我想将列表绑定到数据网格。 为了显示,它工作正常…但是如果我尝试修改一个条目,它说“一个TwoWay或OneWayToSource绑定不能在只读属性’Item1’上工作”……大概是元组在.NET 4.0中是不可变的。 是否有一种简单的方法可以绑定到数据对而无需创建自己的可变元组类?

可以从元组初始化多个变量吗?

在某些语言(例如PHP)中,您可以以类似于以下伪代码的方式自动从元组中分配多个变量: list(string value1, string value2) = tupleWithTwoValues; 我在C#中找不到任何方法可以做到这一点,但是,如果不写更长,更丑陋的代码: string firstValue = tupleWithTwoValues.Item1; string secondValue = tupleWithTwoValues.Item2; 这个两线解决方案显然不是世界末日,但我一直在寻找编写更漂亮代码的方法。 有没有人有更好的方法来做到这一点?

c#LINQ查询填充元组列表

我需要在Linq to Entities的帮助下用MSSQL数据库中的值填充Tuple-List。 下面的代码片段将有助于获取一个Tuple-List,其中1个数据库行反映1个元组条目。 如果calculateData有3行,我们将在使用Field1到Field4创建的列表中获得3个元组条目。 下面的代码使它成为可能: var queryResult = (from a in calculate join b in calculateData on a.Id equals b.CalcId into c where a.SpecialID == 2023 && a.VersionId == 1 orderby a.InternalOrderNr ascending select new { a.Field1, a.Field2, a.Field3, myField4 = c.Select(d => new { d.Field1, d.Field2, d.Field3, d.Field4}) }).ToList(); var result = queryResult.Select(r […]

如何在ASP.NET MVC 4中正确使用元组

我正在尝试使用元组在一个视图中使用两个模型,但我收到此错误:’/’应用程序中的服务器错误。 传递到字典中的模型项的类型为’PagedList.PagedList 1[S00117372CA3.Product]’, but this dictionary requires a model item of type ‘System.Tuple 2 [PagedList.IPagedList 1[S00117372CA3.Product],System.Collections.Generic.IEnumerable 1[S00117372CA3.Product]’, but this dictionary requires a model item of type ‘System.Tuple 1[S00117372CA3.Product],System.Collections.Generic.IEnumerable 1 [S00117372CA3.Order]]’。 描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 exception详细信息:System.InvalidOperationException:传递到字典中的模型项的类型为“PagedList.PagedList 1[S00117372CA3.Product]’, but this dictionary requires a model item of type ‘System.Tuple 2 1[S00117372CA3.Product]’, but this dictionary requires a model item of type […]

如何创建命名引用类型元组?

以下行创建一个命名的ValueTuple : var tuple = (a:1, b:2, c:3, d:4, e:5, f:6); 值类型无法有效传递。 C#7是否提供了一种创建元Tuple类型的命名元组的方法?

在C#中创建不可变类的最简洁方法是什么?

我发现自己必须创建许多不可变类,我想找到一种方法来做到这一点,没有多余的信息。 我不能使用匿名类型,因为我需要从方法返回这些类。 我想要intellisense支持,所以我不想使用词典,动态或类似的东西。 我还想要有名的属性,它排除了元组。 到目前为止,我尝试过一些模式: // inherit Tuple. This has the added benefit of giving you Equals() and GetHashCode() public class MyImmutable : Tuple { public MyImmutable(int field1, string field2, bool field3) : base(field1, field2, field3) { } public int Field1 { get { return this.Item1; } } public string Field2 { get { return […]

C#中的元组和解包分配支持?

在Python中我可以写 def myMethod(): #some work to find the row and col return (row, col) row, col = myMethod() mylist[row][col] # do work on this element 但是在C#中,我发现自己在写作 int[] MyMethod() { // some work to find row and col return new int[] { row, col } } int[] coords = MyMethod(); mylist[coords[0]][coords[1]] //do work on this element […]

惊人的元组(in)平等

直到今天,我对.NET Tuple类的理解是他们将Equals()的实现委托给他们的内容,允许我将它们等同并“按值”进行比较。 然后这个测试出现了,让我变得愚蠢: [TestMethod] public void EquateTwoTuplesWithSameContent() { var t1 = Tuple.Create(“S”); var t2 = Tuple.Create((object)t1.Item1); Assert.IsTrue(t1.Equals(t2)); // Boom! } 阅读MSDN文档和各种博客给我留下了更多问题。 从我收集的内容来看,似乎Tuple和Tuple总是被认为是不相等的,无论两个实例都可以包装同一个对象(盒装或类型转换 – 它都是相同的)。 这真的是Tuples应该如何表现吗? 结构兼容性实际上是对平等的另一个限制,而不是放松,因为我一直在解释它吗? 如果是这样,BCL中还有什么可以用来满足上述unit testing的期望吗? 先感谢您!