gpt4 book ai didi

c# - 使用LINQ删除记录时出错

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:57 24 4
gpt4 key购买 nike

我尝试将SQL Delete命令更改为LINQ。这是我的SQL命令:

DELETE FROM [TrackPoints]
WHERE [RouteFK] IN (SELECT RouteId
FROM Routes
WHERE UserId = @UserId)


这是我编写的LINQ代码:

int UID =1;
FirstDataContext aspnetdb = new FirstDataContext();
var res1 = from q1 in aspnetdb.Routes
where q1.UserId == UID
select q1.RouteId;
foreach (var k in res1)
{
var eigen = from p in aspnetdb.Trackpoints
where p.RouteFK == k
select p.TrackPointId;

aspnetdb.Trackpoints.DeleteOnSubmit(eigen.First());
aspnetdb.SubmitChanges();
}


但在这一行中我有一个错误:

aspnetdb.Trackpoints.DeleteOnSubmit(eigen.First());


那说:


  'System.Data.Linq.Table.DeleteOnSubmit(LINQ_Test.Trackpoint)'的最佳重载方法匹配具有一些无效的参数


我该怎么办?

最佳答案

用这个:

var eigen = (from p in aspnetdb.Trackpoints
where p.RouteFK == k
select p).First();


或更短:

var eigen = aspnetdb.Trackpoints.First(p => p.RouteFK == k);


因为它返回 Trackpoint

而您的 select p.TrackPointId返回 Int



var r = from r in aspnetdb.Routes
join p in aspnetdb.Trackpoints on p.RouteId equals r.RouteFK
where r.UserId == UID
select p;

foreach (var x in r)
{
aspnetdb.Trackpoints.DeleteOnSubmit(x);
}
aspnetdb.SubmitChanges();

关于c# - 使用LINQ删除记录时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11291611/

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