2013-08-07 22 views
0

我是新来的VB.Net来自MS Access背景。我正在研究一个使用linq和SQL后端的程序。联系人有一个数据上下文。linq字段使用条件标准

也许这个例证将澄清。这就是被移交给我:

联系表:

ContactID (PK) 

a_ContactTypes表:

ContactTypeID (PK) 
ContactType 

ContactTypes结表:

ContactTypeID (PK) 
aContactTypeID 
ContactID 

比方说,我有以下Contacts表中的物品:

ContactID 
--------- 
Contact1 
Contact2 
Contact3 

而在A_ ContactTypes表以下项目:

ContactTypeID ContactType 
------------- ----------- 
Type1   Business 
Type2   Private 

这是什么ContactTypes结表如下所示:

ContactTypeID ContactID aContactTypeID 
------------- --------- -------------- 
1    Contact1 Type1 
2    Contact1 Type2 
3    Contact3 Type2 

所以Contact1具有联系人类型PrivateBusinessContact2尚无任何联系方式,并且Contact3只有联系型号Private。 (该ContactTypeID两个表中意味着两个不同的东西是有点混乱,但它是我递了很多,一个小小的不便)

什么我能够进入一个网格是使用LINQ如下:

Contact1 Business 
Contact1 Private 
Contact2 
Contact3 Private 

我想进入的LINQ网格是:

ID   Business Private 
-------- -------- ------- 
Contact1 True  True 
Contact2 False  False 
Contact3 False  True 

所以我简单地创建了一堆的联系对象,并使用每个循环,填补了接触类型还是有在LINQ查询本身内部完成这个工作的好方法吗?免责声明:我再说一遍,我正从一个主要的MS Access VBA背景转移,所以我的术语(和整体知识)可能会关闭。我已经搜索过,但似乎没有什么符合我想要做的,但我可能在搜索时没有使用正确的术语(和整体知识)。我也尝试将这个例子放入列表中,但它看起来并不像我正确应用星号。

+0

使用编辑器中的代码按钮来格式化_code_块。 http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks –

+0

请至少努力设置您的帖子的格式。输入文本区域右上方的橙色“{?}”按钮说明如何操作。另外,请不要在标题中加入标签信息。这里的标记系统非常擅长对事物进行分类,并且不需要帮助。 :-)请参阅[应的问题包括“标签”,在他们的头衔?(http://meta.stackexchange.com/q/19190/172661)。谢谢。:-) –

+0

谢谢肯 - 我进去这样做,它告诉我你已经编辑过。我很抱歉。 –

回答

0

看一看这段代码(Sourced from here)converted。您可以使用它:

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 
+0

嗨,蒙蒂。这看起来像医生所订购的。我从来没有使用过Dictionary类,但是我做了一些研究,看起来它应该完全按照我的需要来做。我会明天尝试一些代码,并让你知道它是如何发生的。非常感谢你指引我朝这个方向发展。 –