Note that there are some explanatory texts on larger screens.

plurals
  1. POTried converting sql query to linq, however, I get different results
    primarykey
    data
    text
    <p>I am trying to convert an sql statement to linq, but maybe I am doing something wrong because I'm getting different results. I am trying my linq statement in LinqPad.</p> <p><b>Here is my sql statment:</b></p> <pre><code>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 </code></pre> <p><b> Linq statment: </b></p> <pre><code>var recIdList = new List&lt;int?&gt;() { 25,32}; var query = from u in TestTables where ( recIdList.Contains(u.Rec_id) &amp;&amp; !(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(); </code></pre> <p>The sql query generated by Linq:</p> <pre><code>-- 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 </code></pre> <p>TestTable:</p> <pre><code>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 </code></pre> <p>The result for my Sql query: set_id 11 <br/> The result for my Linq query: set_id (10 and 11)</p> <p>The idea is I am trying to get the set_id where the the rec_id is exactly (set_id 11: (25,32)), however (set_id 10: (1,25,32)) is not an exact match.</p> <p>I've added two images that shows the result set for both using LinqPad:</p> <p><a href="https://skydrive.live.com/?cid=3A6FFF199D5185F9&amp;id=3A6FFF199D5185F9!151&amp;sc=photos" rel="nofollow">SqlStatmentWithResultSet</a> <br/> <a href="https://skydrive.live.com/?cid=3a6fff199d5185f9#!/?cid=3a6fff199d5185f9&amp;sc=photos&amp;uc=2&amp;id=3A6FFF199D5185F9!150!cid=3A6FFF199D5185F9&amp;id=3A6FFF199D5185F9!152&amp;sc=photos" rel="nofollow">LinqStatmentWihtResultSet</a></p> <p>Thanks in advance for you help...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload