linq查询表达式"左外连接"如果右侧为空,则使用默认值

时间:2014-12-01 15:11:32

标签: .net linq

我控制了我自己制作的数据网格中的列的可见性和顺序(从头开始)。这是用户控件的自定义对象模型。我想返回所有列用于显示目的,但是按照某些列布局类指定的顺序,并且仅当相应的列布局的visible属性为true时才返回。我在异常的基础上配置布局,这样我只需要指定需要订购或隐藏的列。

BrowsableProperties只是一个获取某个类型属性子集的属性,我将其保存在Schema.Type中,并且它是列集的来源。存储在GridLayout属性中的Layout包含许多TypeLayout个,其中包含许多ColumnLayout个。 TypeLayout有一个类型。存在TypeLayout,因为同一网格可以在其生命周期内为不同类型加载许多集合,并相应地更改列。它是一个通用网格。

ColumnLayout与属性一起出现,但每个属性可能没有ColumnLayout。如果没有,我想使用ColumnLayout返回的ColumnLayout.Default,但我无法获得"左外连接,但如果它为空使用此默认值"逻辑工作。

以下列的提取功能只返回内部连接,我无法找到可以插入默认逻辑或左连接逻辑的位置。

  Private Function GetColumns() As List(Of Column)
    Return (
        From PropertyInfo As PropertyInfo
        In BrowsableProperties
        From TypeLayout As TypeLayout In Layout.Types
        From ColumnLayout As ColumnLayout In TypeLayout.Columns
        Where TypeLayout.Type Is Schema.Type AndAlso
        ColumnLayout.Name = PropertyInfo.Name AndAlso
        ColumnLayout.Visible
        Select PropertyInfo,
        ColumnLayout
        Order By
          ColumnLayout.Order,
          PropertyInfo.Name).
        Select(Function(x, i) New Column(Me, i, x.PropertyInfo)
    ).ToList
  End Function

这是GridLayout班级:

<Serializable()> _
Public Class GridLayout
  Inherits DatabaseObject

  <XmlAttribute()> _
  Public Property Context As String

  Private _Types As List(Of TypeLayout)
  Public ReadOnly Property Types As List(Of TypeLayout)
    Get
      If _Types Is Nothing Then
        _Types = New List(Of TypeLayout)
      End If
      Return _Types
    End Get
  End Property

End Class

这是TypeLayout班级:

<Serializable()>
Public Class TypeLayout
  Inherits DatabaseObject

  Private _Columns As List(Of ColumnLayout)
  Public ReadOnly Property Columns As List(Of ColumnLayout)
    Get
      If _Columns Is Nothing Then
        _Columns = New List(Of ColumnLayout)
      End If
      Return _Columns
    End Get
  End Property

  <XmlIgnore()> _
  Public Property Type As System.Type

  <XmlAttribute("Type")> _
  Public Property TypeName As String
    Get
      Return Type.AssemblyQualifiedName
    End Get
    Set(value As String)
      Type = Type.GetType(value)
    End Set
  End Property

End Class

ColumnLayout类:

<Serializable()>
Public Class ColumnLayout
  Inherits DatabaseObject

  <XmlAttribute()> _
  Public Property Name As String

  <XmlAttribute()> _
  Public Property Order As Integer = Integer.MaxValue

  <XmlAttribute()> _
  Public Property [ReadOnly] As Boolean = False

  <XmlAttribute()> _
  Public Property Visible As Boolean = True

  Public Shared ReadOnly [Default] As New ColumnLayout With
    {.Order = Integer.MaxValue,
    .ReadOnly = False,
    .Visible = True}

End Class

这是一个典型的配置:

<GridLayout xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Context="frmApplication/TabControl1/tpHelpDesk/HelpDesk1/grdTickets">
<Types>
<TypeLayout Type="Simmons.BusinessObjects.HelpDesk.Ticket, Simmons.BusinessObjects, Version=6.0.0.0, Culture=neutral, PublicKeyToken=null">
<Columns>
<ColumnLayout Name="TID" Order="0" ReadOnly="false" Visible="true"/>
<ColumnLayout Name="DatabaseState" Order="2147483647" ReadOnly="false" Visible="false"/>
<ColumnLayout Name="Loaded" Order="2147483647" ReadOnly="false" Visible="false"/>
</Columns>
</TypeLayout>
</Types>
</GridLayout>

1 个答案:

答案 0 :(得分:0)

我必须在两个陈述中这样做。我在一个声明中如何做到的原始问题仍然没有答案。

(抱歉vb。语言翻译器应该用于构造程序 - 但是有太多的linq来解码我的Matrix)

  Private Function GetColumns() As List(Of Column)
    Dim ColumnLayouts = From TypeLayout As TypeLayout In Layout.Types
                        From ColumnLayout As ColumnLayout In TypeLayout.Columns
                        Where TypeLayout.Type = Schema.Type
                        Select ColumnLayout

    Return (From PropertyInfo As PropertyInfo
          In BrowsableProperties
          Group Join ColumnLayout As ColumnLayout In ColumnLayouts.DefaultIfEmpty(ColumnLayout.Default)
          On PropertyInfo.Name Equals ColumnLayout.Name
          Into ColumnLayoutDefaults = Group
          From ColumnLayout In ColumnLayoutDefaults.DefaultIfEmpty(ColumnLayout.Default)
          Where ColumnLayout.Visible
          Order By ColumnLayout.Order).
          Select(Function(FromClause, Index) New Column(Me, FromClause.PropertyInfo, FromClause.ColumnLayout, Index)).
          ToList

  End Function