none
Repeated assignments overwrite a field in query RRS feed

  • General discussion

  • Hi all,

    I'm having this weird issue, look at my code:

    var query = from rud in db.DBRud select rud;
    
    foreach(myObject p in objects)
    {
        switch(p.type)
        {
            case "rudobject":
               //p.code now is "linea_di_prodotti"
               query = from r in query  
                  join s in db.ValueRudobject on r.id equals s.rud_id
                  where s.field_code == p.code && values.Contains(s.value) select r;
             break;
          
            case "checkbox":
                //p.code now is "us"
                query = from r in query
                   join c in db.ValueCheckbox on r.id equals c.rud_id
                   where c.field_code == p.code && values.Contains(c.value) select r;
            break;
    
        }
    }




    so, the query is built dynamically. But if you look at the sql produced, you'll notice that the second assignment overwrites the variable p.code! Look:
    SELECT [t0].[id], [t0].[user] AS [user], [t0].[name], [t0].[status], [t0].[notes], [t0].[rudobject], [t0].[internal_name], [t0].[visible], [t0].[created], [t0].[modified], [t0].[locale], [t0].[metadescription], [t0].[keywords], [t0].[referer], [t0].[folder], [t0].[position]
    FROM [dbo].[ruds] AS [t0]
    INNER JOIN [dbo].[ruds_values_checkbox] AS [t1] ON [t0].[id] = [t1].[rud_id]
    INNER JOIN [dbo].[ruds_values_rudobject] AS [t2] ON [t0].[id] = [t2].[rud_id]
    WHERE
        ([t2].[field_code] = @p0) AND
        ([t2].[value] IN (@p1, @p2)) AND
        ([t1].[field_code] = @p3) AND
        ([t1].[value] IN (@p4)) AND
        ([t0].[locale] = @p5) AND
        ([t0].[rudobject] = @p6)
      
      
        -- @p0: Input VarChar (Size = 17; Prec = 0; Scale = 0) [linea_di_prodotto]
        -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [42]
        -- @p2: Input Int (Size = 0; Prec = 0; Scale = 0) [43]
        -- @p3: Input NVarChar (Size = 17; Prec = 0; Scale = 0) [linea_di_prodotto]  ERROR !!! should be "us" !!!!!!!!!!
        -- @p4: Input NVarChar (Size = 13; Prec = 0; Scale = 0) [International]
        -- @p5: Input VarChar (Size = 5; Prec = 0; Scale = 0) [it_IT]
        -- @p6: Input VarChar (Size = 8; Prec = 0; Scale = 0) [prodotto]
        -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1    




    It's for sure a question of variables overwriting, because if I change the order of the items inside the variable objects (the one into the foreach) I got "us " duplicated, instead of "linea_di_prodotto ". So, the last value in p.code overwrites the value of all the other previous p.code.

    What do you think about? Do you have any suggestion for a workaround?

    thanks
    darko

    Tuesday, November 24, 2009 3:45 PM

All replies

  • Hi,

    I was able to solve the issue moving the assignments in a new method, so the references are lost and a new object is instanciated (but it'll eat more memory).

    case "rudobject":
    			query = addPropertyCondition(query, p.type, p.code, p.value);
    	break;
    case "checkbox":
    	query = addPropertyCondition(query, p.type, p.code, p.value);
    	break;

    (now I don't need no more a switch(), I leave it to let you see what I have done)

    and the method:

    private IQueryable<DBRud> addPropertyCondition(IQueryable<DBRud> query, string type, string field, object value)
    { 
    	switch(type)
    	{
    		case "rudobject":
    			List<int> vr = (List<int>)value;
    			return from r in query
    					join s in db.ValueRudobject on r.id equals s.rud_id
    					where s.field_code == field && vr.Contains(s.value)
    					select r;
    
    		case "checkbox":
    			List<string> vc = (List<string>)value;
    			return from r in query
    				   join s in db.ValueCheckbox on r.id equals s.rud_id
    				   where s.field_code == field && vc.Contains(s.value)
    				   select r;
    	}
    	throw new Exception("Type " + type + " is not managed");
    }

    Tuesday, November 24, 2009 4:37 PM