Page 1 of 1

Parameter Problem when Using Union

Posted: Sun 11 Jul 2010 17:56
by daveoggy
I have the following code which I think exposes a bug:

Code: Select all

            Dictionary> Lookup = new Dictionary>();
            Lookup.Add(200, new List() { 0, 1, 2 });
            Lookup.Add(330, new List() { 0, 1, 4 });

            using (Entities ent = new Entities())
            {
                IQueryable query = null;
                foreach (var x in Lookup)
                {
                    var queryPart = from t in ent.TM_FNET
                                    where t.ACTION.Value == x.Key
                                    && x.Value.Contains(t.STATUS.Value)
                                    select t.ID;

                    //first one becomes the main query, subsequent ones union-ed on...
                    if (query == null)
                        query = queryPart;
                    else
                        query = query.Union(queryPart);
                }

                foreach (var f in query.ToList())
                {
                    Console.WriteLine("{0}", f);
                }
            }
Which results in the following SQL statement:

Code: Select all

SELECT 
"Distinct1".C1 AS C1
FROM ( SELECT DISTINCT 
	"UnionAll1".ID AS C1
	FROM  (SELECT 
		"Extent1".ID AS ID
		FROM TMDDBA.TM_FNET "Extent1"
		WHERE ("Extent1".ACTION = :p__linq__0) AND (((0 = "Extent1".STATUS) OR (1 = "Extent1".STATUS)) OR (4 = "Extent1".STATUS))
	UNION ALL
		SELECT 
		"Extent2".ID AS ID
		FROM TMDDBA.TM_FNET "Extent2"
		WHERE ("Extent2".ACTION = :p__linq__1) AND (((0 = "Extent2".STATUS) OR (1 = "Extent2".STATUS)) OR (4 = "Extent2".STATUS))) "UnionAll1"
)  "Distinct1"
All is well expect for the values assigned to the parameters :p_linq_0 and p_linq_1, for some reason both of these values are 330.

Posted: Mon 12 Jul 2010 15:15
by AndreyR
I have reproduced this issue as well. The investigation is in progress.
I will notify you about the results of our investigation as soon as possible.

Posted: Fri 16 Jul 2010 16:38
by daveoggy
Thanks AndreyR, any progress?

Posted: Wed 21 Jul 2010 15:57
by AndreyR
Our investigation is in progress, sorry for the delay.
I will post here as soon as the results are available.

Posted: Fri 23 Jul 2010 14:58
by AndreyR
This situation is a bit more complicated.
In fact, everything goes correct.
The binding of the EF parameter occurs at the moment you materialize the query, not at the moment when query is initialized.
So, value of x.Key is taken from the latest foreach iteration. This is a designed behaviour, the solution is to use different variables for different parameters.