1

I have issue with Include Method , scenario as given

Table Warehouse has columns

  1. Id--> unique identifier and PK
  2. warehouseNumber--nvarchar(50)

--some more columns

Table WarehouselnkedEcorders

  1. Id-->PK,unique Identifier
  2. warehouseUniqueId-->(FK,Unique Identifier), It has relationship with "Warehouse" table Id column
  3. Status

Warehouse Model has following Code

public class Warehouse
{
    public Warehouse()
    {
        this.WarehouselnkedEcorders = new List<WarehouselnkedEcorder>();
    }
    //Some stuff

    public virtual ICollection<WarehouselnkedEcorder> WarehouselnkedEcorders 
    { get; set; }
}

WarehouselnkedEcOrdeMap has following Code

        // Relationships
        this.HasOptional(t => t.Warehouse)
            .WithMany(t => t.WarehouselnkedEcorders)
            .HasForeignKey(d => d.warehouseUniqueId);

Query

I need to retrive a list of warehouse entity with status=true. I tried the following way,but could not get result. How can I do this?

  List<Warehouse> lstObjWarehouse = objWMSContext.Warehouses.Include("WarehouselnkedEcorders").Where(//o=>SomeCondition)
                                                       .Where(o => SomeCondition)
                                                       .Where(o => o.Deleted == false).ToList();

Here I need to compare value of status=true of each row of "WarehouselnkedEcorder" and it should return result.

4
  • 2
    How many of a Warehouse's related WarehouselnkedEcorders must have a true Status to include the Warehouse in the result set? Commented Dec 19, 2012 at 13:26
  • have you tried "WarehouselnkedEcorders" instead of "WarehouselnkedEcorder"? Commented Dec 19, 2012 at 13:28
  • @lante: I have included "WarehouselnkedEcorders". Commented Dec 19, 2012 at 13:59
  • in your code, you didn't write the "s" in the Include statment. if so, please edit Commented Dec 19, 2012 at 14:01

2 Answers 2

1

Use Any in query:

List<Warehouse> lstObjWarehouse = db.Warehouses.Include("WarehouselnkedEcorder")
.Where(o => o.invoicePath == "SomeCondition" 
&& o.Deleted == false 
&& o.WarehouselnkedEcorders.Any(p => p.id == o.id 
&& p.Deleted == false)).ToList();
Sign up to request clarification or add additional context in comments.

1 Comment

It works by changing navigation property name to "WarehouselnkedEcorders"
0

Fixed to match 1:many condition

List<Warehouse> lstObjWarehouse =   objWMSContext.Warehouses.Include("WarehouselnkedEcorder")
                    .Where(o => o.somefield == "SomeCondition"
                  &&       o => o.Deleted == false
                  &&       o=>o.WarehouselnkedEcorders.Where(dep=>dep.status==true)
                 .ToList();

1 Comment

It supposed to get o=>o.WarehouselnkedEcorders.status == true. But it shows WarehouselnkedEcorders as a list with methods like Any,Where,etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.