问题随着LINQ和参考名单(Issue With LINQ and Reference List)

2019-09-16 09:53发布

我有以下的LINQ代码

var q = (from web in DataContext.Webs select web);
List<int?> k1 = new List<int?>() { 1, 2 };
List<int?> k2=new List<int?>() { 16, 17 };
            q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId)));
            System.Diagnostics.Debug.WriteLine(q.Count());
            k1 = k2;
            q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId)));
            System.Diagnostics.Debug.WriteLine(q.Count());

以下是查询它产生。

计数的第一次执行。

SELECT COUNT(*) AS [value]
FROM [dbo].[Web] AS [t0]
WHERE EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[WebTechMap] AS [t1]
    WHERE ([t1].[WebsiteId] = ([t0].[WebsiteId])) AND ([t1].[TechId] IN (@p0, @p1))
    )
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [2]

计数功能的第二次执行

SELECT COUNT(*) AS [value]
FROM [dbo].[Web] AS [t0]
WHERE (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[WebTechMap] AS [t1]
    WHERE ([t1].[WebsiteId] = ([t0].[WebsiteId])) AND ([t1].[TechId] IN (@p0, @p1))
    )) AND (EXISTS(
    SELECT NULL AS [EMPTY]
    FROM [dbo].[WebTechMap] AS [t2]
    WHERE ([t2].[WebsiteId] = ([t0].[WebsiteId])) AND ([t2].[TechId] IN (@p2, @p3))
    ))
-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [17]
-- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [17]

请看看传递给查询参数。

似乎它使用两个表达式的新引用的对象。

不知怎的,@ p0和@ P1改变自己的旧值。 据我所知,问题是Linq中它采用新的引用对象。

-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [2]



-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [17]
-- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [17]

有人能解释如何使用List对象的名称相同,但采用正确的列表的保留查询

所需的输出是

-- @p0: Input Int (Size = -1; Prec = 0; Scale = 0) [1]
-- @p1: Input Int (Size = -1; Prec = 0; Scale = 0) [2]
-- @p2: Input Int (Size = -1; Prec = 0; Scale = 0) [16]
-- @p3: Input Int (Size = -1; Prec = 0; Scale = 0) [17]

提前致谢

Answer 1:

你的第一个查询表达式使用k1 。 它使用可变 kl ,而那正是抓获。 当您更改的值k1 ,这是有效地改变查询的含义。 如果你不希望查询的意思改变,不改变捕获变量! 只需使用k2在你的第二个过滤器:

var q = (from web in DataContext.Webs select web);
List<int?> k1 = new List<int?>() { 1, 2 };
List<int?> k2=new List<int?>() { 16, 17 };
q = q.Where(web => DataContext.WebTechMaps
                              .Any(t => t.WebsiteId == web.WebsiteId && 
                                        k1.Contains(t.TechId)));
System.Diagnostics.Debug.WriteLine(q.Count());
q = q.Where(web => DataContext.WebTechMaps
                              .Any(t => t.WebsiteId == web.WebsiteId &&
                                   k2.Contains(t.TechId)));
System.Diagnostics.Debug.WriteLine(q.Count());


文章来源: Issue With LINQ and Reference List