【问题标题】:SQL Linq join List VB NETSQL Linq 连接列表 VB NET
【发布时间】:2017-03-03 13:24:07
【问题描述】:

Tabella_Pratiche 是我的数据库的实体,P_ListLettore 是对象列表。 当我尝试加入 Tabella_PraticheP_ListLettore 时,我会产生异常。

Dim listPraticheEsistenti As List(Of Tabella_Pratiche)

listPraticheEsistenti = (From c In DB.Tabella_Pratiche Join b In P_ListLettore On c.posizione Equals b.AssignamentID And                c.data_affido.ToString("dd/MM/yyyy") Equals b.R_RecordPA.Data_inizio_affidamento.ToString("dd/MM/yyyy") Where c.tipo_mandato = "SPG" Select c).ToList

例外“Eccezione non gestita di tipo EntityFramework.SqlServer.dll 中的“System.NotSupportedException”

Ulteriori information: Impossibile creare un valore costante di tipo 'ImportPratiche.RecordPrSPG'。 In questo Competitiono sono supportati solo Tipi primitivi o enumerazione。 "

【问题讨论】:

  • 以后请提供英文异常信息。或者,如果您更喜欢用西班牙语交流,也可以使用Spanish SO(我知道这是意大利语,但我知道的只有这些)。该错误是因为您不能在内存列表和将使用 Linq to Sql 访问的列表之间使用连接语法(仍在数据库中)。如果你想要一个接近的等价物,你可以使用Contains,它受支持并将被翻译成数据库上的IN子句,但这仅支持简单类型的列表,如intstring的列表。跨度>
  • 你能举个例子吗?
  • 您也不能使用 ToString(""),这是一个 .net 概念,不会转换为 sql。这可能就是问题所在。我不会读意大利语,所以我在这里猜测,您的代码中可能还有其他问题。
  • 我试过不使用 tostring 但我遇到了同样的问题

标签: sql vb.net linq join


【解决方案1】:

就像@Igor 在他的评论中所写,您不能将内存中的对象列表与数据库结果连接起来。
这是因为 Entity Framework 尝试将 LINQ 转换为 SQL 语句,而 P_ListLettore 在您的数据库中是未知的,因此 Entity Framework 是未知的。

你可以做的是先从数据库中加载数据,然后在内存中“加入”(以下代码完全未经测试):

    'IEnumerable with AssignamentIDs'
    Dim assignmentIDs = From p In P_ListLettore
                        Select p.AssignamentID

    'Load Tabella_Pratiche with matching IDs from DB and convert into in-memory List'
    Dim tmpPratiche = (From c In DB.Tabella_Pratiche
                       Where c.tipo_mandato = "SPG" And assignmentIDs.Contains(c.posizione)
                       Select c).ToList()

    ' Perform Join on both in-memory lists with ID and Date'
    Dim listPraticheEsistenti = (From c In tmpPratiche
                                 Join b In P_ListLettore On c.posizione Equals b.AssignamentID 
                                      And c.data_affido Equals b.R_RecordPA.Data_inizio_affidamento
                                  Select c).ToList

请注意,您只能在 Linq2Sql 查询中使用一小组 .NET 函数,例如,Date.ToString(format) 将不起作用。

【讨论】:

  • 出现问题 Error Can not convert the value of type ' (line 130)' in '
  • 我不想在数据库中加载 P_LISTALETTORE
  • 您不必加载 P_LISTALETTORE。你必须加载所有Tabella_Pratiche,循环P_LISTALETTORE并检查posizionedata_affido是否等于AssignamentIDData_inizio_affidamento。这会更慢,但应该可以工作。周一我回到办公室时会编辑我的答案
  • 顺便说一句...对不起,我发布了一个不正确的答案。通常我会在发布之前测试我的代码...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-07
相关资源
最近更新 更多