Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 使用条件条件条件的linq字段_Vb.net_Linq - Fatal编程技术网

Vb.net 使用条件条件条件的linq字段

Vb.net 使用条件条件条件的linq字段,vb.net,linq,Vb.net,Linq,我是VB.Net的新手,有MS Access的背景。我正在开发一个使用linq和SQL后端的程序。存在联系人的数据上下文 也许这个例子能说明问题。这是交给我的: 联系人表: ContactID (PK) 联系人类型表: ContactTypeID (PK) ContactType 触点类型连接表: ContactTypeID (PK) aContactTypeID ContactID 假设我在联系人表中有以下项目: ContactID --------- Contact1 Contact2

我是VB.Net的新手,有MS Access的背景。我正在开发一个使用linq和SQL后端的程序。存在联系人的数据上下文

也许这个例子能说明问题。这是交给我的:

联系人表:

ContactID (PK)
联系人类型表:

ContactTypeID (PK)
ContactType
触点类型连接表:

ContactTypeID (PK)
aContactTypeID
ContactID
假设我在
联系人
表中有以下项目:

ContactID
---------
Contact1
Contact2
Contact3
ContactTypeID   ContactType
-------------   -----------
Type1           Business
Type2           Private
以及a联系人类型表中的以下项目:

ContactID
---------
Contact1
Contact2
Contact3
ContactTypeID   ContactType
-------------   -----------
Type1           Business
Type2           Private
这就是ContactTypes连接表的外观:

ContactTypeID   ContactID  aContactTypeID
-------------   ---------  --------------
1               Contact1   Type1
2               Contact1   Type2
3               Contact3   Type2
因此
Contact1
有联系人类型
Private
Business
Contact2
没有联系人类型,
Contact3
只有联系人类型
Private
。(ContactTypeID的
意思是两张表中的两个不同的东西,这有点让人困惑,但这是我拿到的那批东西,还有一点小麻烦)

通过Linq,我能够进入网格的内容如下:

Contact1   Business
Contact1   Private
Contact2   
Contact3   Private
我想通过Linq进入网格的是:

ID         Business    Private
--------   --------    -------
Contact1   True        True
Contact2   False       False
Contact3   False       True
那么,我是简单地创建一组联系人对象并使用for-each循环来填充联系人类型,还是在linq查询本身中有一种好的、简洁的方法来完成这一工作

免责声明:我重申,我的背景主要是MS Access VBA,因此我的术语(和总体知识)可能不适用。我已经搜索过了,但似乎没有什么适合我尝试做的,但我可能是在没有正确的术语(和总体知识)的情况下搜索的。我还尝试将示例放入列表中,但看起来我没有正确地应用星号。

请查看此代码并进行修改。您可以使用它:

Imports System
Imports System.Collections.Generic
Imports System.Linq

Namespace ReflectionIT.Training

    Public NotInheritable Class LinqExtenions
        Private Sub New()
        End Sub

        <System.Runtime.CompilerServices.Extension> _
        Public Shared Function Pivot(Of TSource, TFirstKey, TSecondKey, TValue)(source As IEnumerable(Of TSource), firstKeySelector As Func(Of TSource, TFirstKey), secondKeySelector As Func(Of TSource, TSecondKey), aggregate As Func(Of IEnumerable(Of TSource), TValue)) As Dictionary(Of TFirstKey, Dictionary(Of TSecondKey, TValue))
            Dim retVal = New Dictionary(Of TFirstKey, Dictionary(Of TSecondKey, TValue))()

            Dim l = source.ToLookup(firstKeySelector)
            For Each item As var In l
                Dim dict = New Dictionary(Of TSecondKey, TValue)()
                retVal.Add(item.Key, dict)
                Dim subdict = item.ToLookup(secondKeySelector)
                For Each subitem As var In subdict
                    dict.Add(subitem.Key, aggregate(subitem))
                Next
            Next

            Return retVal
        End Function

    End Class
End Namespace
导入系统
导入System.Collections.Generic
导入系统
名称空间反射训练
公共不可继承类LinqExtensions
私人分新
端接头
_
公共共享函数Pivot(属于TSource,TFirstKey,TSecondKey,TValue)(源为IEnumerable(属于TSource),第一个KeySelector作为Func(属于TSource,TFirstKey),第二个KeySelector作为Func(属于TSource,TSecondKey),聚合为Func(属于IEnumerable(属于TSource,TValue))作为字典(属于TFirstKey,属于TSecondKey,TValue))
Dim retVal=新字典(属于TFirstKey,字典(属于TSecondKey,TValue))()
Dim l=源.ToLookup(firstKeySelector)
对于每个项目,作为l中的var
Dim dict=新字典(属于TSecondKey,TValue)()
retVal.Add(item.Key,dict)
Dim子目录=item.ToLookup(第二个按键选择器)
对于每个子项,作为子项中的变量
dict.Add(子项.键,聚合(子项))
下一个
下一个
返回返回
端函数
末级
结束命名空间

使用编辑器中的“代码”按钮格式化代码块。请至少努力格式化你的帖子。您要输入的文本区域右上角上方的橙色
{?}
按钮解释了如何执行此操作。另外,请不要在标题中添加标签信息。这里的标签系统非常擅长分类,不需要帮助。:-)请看。谢谢。:-)谢谢你,肯,我进去了,它告诉我你已经编辑过了。很抱歉。你能发布你当前的LINQ查询吗?嗨,蒙蒂。这看起来正是医生要的。我从未使用过Dictionary类,但我做了一些研究,它看起来应该完全满足我的需要。我明天会尝试一些代码,让你知道它是如何运行的。非常感谢你为我指明了这个方向。