Make a query with LINQ to entities

0

var query_actualizar = from x in ctx.banner
			let orden = ctx.banner.Where(y.id_banner == id_banner).First(y=> y.orden)
			where x.orden > orden
			select x;


foreach (banner ord in query_actualizar)
{
   ord.orden = ord.orden - 1;
}

Good morning I would like to make a query in LINQ: SQL = > Update BANNER set ORDER = ORDER - 1 where ORDER > (select ORDER from BANNER where ID_BANNER = 18)

When making the query I get this error:

    
asked by elizabeth 19.12.2016 в 15:27
source

5 answers

2

The problem with your original query is that First what you expect is a conditional that meets the record you want to return. There are several options to do what you ask, one is:

var query_actualizar = from x in ctx.banner
        let orden = ctx.banner.First(y=> y.id_banner == id_banner).orden
        where x.orden > orden
        select x;

Another

var query_actualizar = from x in ctx.banner
        let orden = ctx.banner.Where(y.id_banner == id_banner).Select(y=>y.orden).FirstOrDefault();
        where x.orden > orden
        select x;

Right now I can not prove it, but I think either of you should work. If not, tell me about it.

    
answered by 19.12.2016 / 17:47
source
1

I edit with the new information

Test using the FirstOrDefault method to get the first element of the sequence that meets the condition:

var query_actualizar = from x in ctx.banner
            let orden = ctx.banner.FirstOrDefault(y => y.id_banner == id_banner).orden
            where x.orden > orden
            select x;
    
answered by 19.12.2016 в 15:55
0

Hello, Santiaguito, test your code, and I got the following error, sent the image, please some idea of how to correct this ?, Thanks

    
answered by 19.12.2016 в 17:24
0

Holas Pikoh and Santiaguito, I came out with this code, Gracias!.

var query_actualizar = from x in ctx.banner
        let orden = ctx.banner.First(y=> y.id_banner == id_banner).orden
        where x.orden > orden
        select x;
    
answered by 19.12.2016 в 18:39
0

I think it would be convenient to first evaluate if the result of the query with firstordefault is different from null, before invoking the Where. Inside the library assembly. Indicates:

    // Exceptions:
    //   System.ArgumentNullException:
    //     source or predicate is null.
    
answered by 19.12.2016 в 19:13