English 中文(简体)
然而,三驾车把 s子转换到林克,我取得了不同的结果。
原标题:Tried converting sql query to linq, however, I get different results

我正试图将 statement言改成lin子,但也许我会做一些错误的事情,因为我会取得不同的结果。 我正在林克帕德试图发言。

Here is my sql statment:

SELECT set_id
        FROM TestTable
        WHERE rec_id In (25,32)
        AND set_id NOT IN
        (
        SELECT x.set_id FROM TestTable x
        WHERE x.rec_id NOT IN(25,32)
        )
        GROUP BY set_id
        HAVING COUNT(set_id) = 2

林克:

var recIdList = new List<int?>() { 25,32};

var query = from u in TestTables
                            where
                            (
                                recIdList.Contains(u.Rec_id) &&
                                !(from k in TestTables
                                  where !recIdList.Contains(u.Rec_id)
                                  select k.Set_id).Contains(u.Set_id)
                            )
                            group u by u.Set_id into userGroup
                            where userGroup.Count() == recIdList.Count
                            select userGroup.Key;
query.Dump();
TestTables.Dump();

The sql query generated by Linq:

-- Region Parameters
DECLARE @p0 Int = 25
DECLARE @p1 Int = 32
DECLARE @p2 Int = 25
DECLARE @p3 Int = 32
DECLARE @p4 Int = 2
-- EndRegion
SELECT [t2].[set_id]
FROM (
    SELECT COUNT(*) AS [value], [t0].[set_id]
    FROM [TestTable] AS [t0]
    WHERE ([t0].[rec_id] IN (@p0, @p1)) AND (NOT (EXISTS(
        SELECT NULL AS [EMPTY]
        FROM [TestTable] AS [t1]
        WHERE ([t1].[set_id] = [t0].[set_id]) AND (NOT ([t0].[rec_id] IN (@p2, @p3)))
        )))
    GROUP BY [t0].[set_id]
    ) AS [t2]
WHERE [t2].[value] = @p4

测试:

Create Table TestTable
(
  set_id int,
  rec_id int
)
Insert Into TestTable (set_id, rec_id) Values(10, 1)
Insert Into TestTable (set_id, rec_id) Values(10, 25)
Insert Into TestTable (set_id, rec_id) Values(10, 32)
Insert Into TestTable (set_id, rec_id) Values(20, 61)
Insert Into TestTable (set_id, rec_id) Values(20, 90)
Insert Into TestTable (set_id, rec_id) Values(30, 77)
Insert Into TestTable (set_id,rec_id)  Values(11,25)
Insert Into TestTable (set_id,rec_id)  Values(11,32)

GO

The result for my Sql query: set_id 11
The result for my Linq query: set_id (10 and 11)

想法是,如果补偿金确实存在(第_id 11:25,32页),那么(第10页:(1,25,32页)并不确切。

我增加了两个图像,显示使用LinqPad两种方法的结果:

SqlStatmentWithResultSet
LinqStatmentWihtResultSet

提前感谢你们的帮助......

最佳回答

In your LINQ statement, in the subquery, you have "!recIdList.Contains(u.Rec_id)". Should this not be testing for k.Rec_id?

问题回答

暂无回答




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签