gpt4 book ai didi

linq-to-sql - Linq to SQL - 如何在插入之前验证记录不存在

转载 作者:行者123 更新时间:2023-12-01 13:06:29 25 4
gpt4 key购买 nike

我正在为一个客户开发一个小应用程序,他们向我提供了一个城市列表,我必须将它们插入数据库并将它们与他们的父记录相关联。

例子:

ID   |   PID   |   Region
1 0 California
2 1 Los Angeles
3 1 San Fransisco

现在我的代码是这样的

    Dim input As StreamReader
Dim index As Integer
Dim filename As String
Dim RegionDC As New DAL.RegionsDataContext



For Each TextFile As String In Directory.GetFiles(Server.MapPath("~/app_data/business-trader cities/"))
input = File.OpenText(TextFile)
filename = New FileInfo(TextFile).Name
index = 0


''# this is where we want to select the ID for the filename'
Dim _ID = (From R In RegionDC.bt_Regions _
Where R.Region = filename.Replace(".txt", "") _
Select R.ID).FirstOrDefault

While Not input.EndOfStream

Dim q = (From r In RegionDC.bt_Regions _
Where r.Region = input.ReadLine() _
Select r.ID).FirstOrDefault

''# ***********************************'
''# HERE IS WHERE IM TRYING TO VERIFY'
''# IF THE RECORD EXISTS OR NOT'
''# ***********************************'

''# now we loop through the txt file'
''# and insert the data into the database'
Dim oRegion As New DAL.bt_Region
oRegion.Region = input.ReadLine()
oRegion.FSSearchCount = 0
oRegion.WSearchCount = 0
oRegion.PID = _ID

RegionDC.bt_Regions.InsertOnSubmit(oRegion)
RegionDC.SubmitChanges()

End While

''# clean up the locked files'
input.Close()
input.Dispose()
Next

所以基本上如果 Los Angeles 在 TXT 文件中,我不希望它重新输入数据库,因为它已经存在。

谁能帮我弄清楚如何在插入之前验证记录是否已经存在?

最佳答案

LINQ To SQL Samples - EXISTS/IN/ANY/ALL

Public Sub LinqToSqlExists01()
Dim q = From c In db.Customers _
Where Not c.Orders.Any()

ObjectDumper.Write(q)
End Sub

关于linq-to-sql - Linq to SQL - 如何在插入之前验证记录不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2791377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com