How to add a condition inside or after .Select() using Linq EF

Blue

My issue is simple but I cannot solve this and I have searched over the internet but one of them matches my issue. This is my code:

    public Producto VerificarExTalla(int id, bool related = true)
    {
        var productos = _context.Productos.AsQueryable();

        if (related)
        {
            productos = productos
                .Include(s => s.Categoria)
                .Include(s => s.DetalleTallas)
                .Include(s => s.DetalleTallas.Select(a => a.Talla));
        }

        return productos
            .Where(s => s.Id == id)
            .SingleOrDefault();

    }

I am trying to perform a condition after .Select() . This is what I have tried but did not work:

.Include(s => s.DetalleTallas.Select(a => a.Talla).Where(a => a.EstadoTallaa == false));

And also tried this, inside .Select:

.Include(s => s.DetalleTallas.Select(a => a.Talla.EstadoTallaa == false));

both gives me the same error:

The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path'

Raihan

Looking at your code it appears that you want to achieve following -

  1. Get Productos including related Categoria and DetalleTallas
  2. DetalleTallas list will be filter to include records with EstadoTallaa == false
  3. Get the Productos only with id matched with the parameter value

As a simple logical programmer I would like to write as follows -

productos = productos.Where(p=> p.Id == id)
                     .Include(s => s.Categoria)
                     .Include(s => s.DetalleTallas.Where(d=> d.EstadoTallaa == false));

But sadly it would not work! It will display error "Lambda expression used inside Include is not valid".

So this can be rewritten as like this -

productos = productos.Where(p=> p.Id == id)
                     .Select(p => new Productos
                     {
                        Id = p.Id,
                        Categoria = p.Categoria
                        // some other properties of Productos
                        DetalleTallas = p.DetalleTallas.Where(d => d.EstadoTallaa == false).ToList()
                      });

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to add condition in linq lambda expression

分類Dev

select object which matches with my condition using linq

分類Dev

Using if-else condition inside a insert-select block

分類Dev

Select only specific fields with Linq (EF core)

分類Dev

How to delete space inside the string under condition, using SQL?

分類Dev

How to modify this Select Statement inorder to get the data using LINQ?

分類Dev

How to add class to a tag on different condition using jquery?

分類Dev

How to add an xml tag based on specific condition using python

分類Dev

Linq FirstOrDefault(<condition>)。Attribute vs Where(<condition>)。Select(a => Attribute).FirstOrDefault()

分類Dev

How to filter inside For loop with OR condition?

分類Dev

How to add map inside document in firestore using flutter?

分類Dev

How do I select rows in a data frame before and after a condition is met?

分類Dev

Using list as select inside form

分類Dev

EF Linq Error after change from dotnet Core 2.2.6 to 3.0.0

分類Dev

How to SELECT a value in a 'same' condition?

分類Dev

Select inside a select using lambda expressions

分類Dev

How to add a condition to a match in case ... of

分類Dev

How to add a condition to a variable - GAMS

分類Dev

Querying the min element using LINQ inside Tasks

分類Dev

How to show/hide div element after dynamically loading options in select box inside these div's?

分類Dev

How to add new column to existing table and set it value based on existing column using EF migrations

分類Dev

How to add a line after nth occurrence of a keyword using sed?

分類Dev

Using 'OR' inside a LinQ join query (adapting SQL into LinQ)

分類Dev

C# Linq add element to list of object where condition is met

分類Dev

NOT IN Condition in Linq

分類Dev

Mysql: How to query with if else condition inside if

分類Dev

How to add a new table with EF core

分類Dev

How can I select using LINQ for an entry that contains a LIST with more than one row?

分類Dev

razor MVC EF how to show and hide a div based on model condition

Related 関連記事

  1. 1

    How to add condition in linq lambda expression

  2. 2

    select object which matches with my condition using linq

  3. 3

    Using if-else condition inside a insert-select block

  4. 4

    Select only specific fields with Linq (EF core)

  5. 5

    How to delete space inside the string under condition, using SQL?

  6. 6

    How to modify this Select Statement inorder to get the data using LINQ?

  7. 7

    How to add class to a tag on different condition using jquery?

  8. 8

    How to add an xml tag based on specific condition using python

  9. 9

    Linq FirstOrDefault(<condition>)。Attribute vs Where(<condition>)。Select(a => Attribute).FirstOrDefault()

  10. 10

    How to filter inside For loop with OR condition?

  11. 11

    How to add map inside document in firestore using flutter?

  12. 12

    How do I select rows in a data frame before and after a condition is met?

  13. 13

    Using list as select inside form

  14. 14

    EF Linq Error after change from dotnet Core 2.2.6 to 3.0.0

  15. 15

    How to SELECT a value in a 'same' condition?

  16. 16

    Select inside a select using lambda expressions

  17. 17

    How to add a condition to a match in case ... of

  18. 18

    How to add a condition to a variable - GAMS

  19. 19

    Querying the min element using LINQ inside Tasks

  20. 20

    How to show/hide div element after dynamically loading options in select box inside these div's?

  21. 21

    How to add new column to existing table and set it value based on existing column using EF migrations

  22. 22

    How to add a line after nth occurrence of a keyword using sed?

  23. 23

    Using 'OR' inside a LinQ join query (adapting SQL into LinQ)

  24. 24

    C# Linq add element to list of object where condition is met

  25. 25

    NOT IN Condition in Linq

  26. 26

    Mysql: How to query with if else condition inside if

  27. 27

    How to add a new table with EF core

  28. 28

    How can I select using LINQ for an entry that contains a LIST with more than one row?

  29. 29

    razor MVC EF how to show and hide a div based on model condition

ホットタグ

アーカイブ