Solved

Using EF and included tables

Posted on 2013-05-31
3
228 Views
Last Modified: 2013-06-08
Experts,
Ok, so here is the scenario:
Assume that EF is initialized and has a good context to retrieve data from a database.  The database contains Tables called ObjectA and ObjectB and has data in them.

Check out the following code snippet:
Class ObjectA
{
	int ID;
	string Name;
	IEnumerable<ObjectB> objectB;
}

class ObjectB
{
	int ID;
	string Name;
	bool IsDeleted;
}

public IEnumerable<ObjectA> GetAllObjects(bool includeDeletedObjectB = false)
{
	var myobjects = GetAll()
		       .Include("ObjectB");
	if(!includeDeletedObjectB)
	{
                foreach(var item in myobjects)
                {
                    item.ObjectB.Where(y => y.IsDeleted == false);
                }
	}
	return myobjects;
}

Open in new window


Question:  Is there a way to pass in a parameter into the GetAllObjects, and somewhere in the var myobjects query, test the boolean includeDeletedObjectB and if true, include all sub ObjectBs (IsDeleted is true or false), but if false, only include those ObjectB whose IsDeleted flag is false.

Your assistance is greatly appreciated!
0
Comment
Question by:ericlockshine
  • 2
3 Comments
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
Hi ericlockshine;

This code snippet will do what you need

public IEnumerable<ObjectA> GetAllObjects(bool includeDeletedObjectB = false)
{
    // Set up the base query but don't execute it yet.
    var myObjects = from oA in DataContextInstance.ObjectA.Include("ObjectB")
                    select oA;
    
    // add the correct where clause to the above query
    if( includeDeletedObjectB )
        myObjects = myObjects.Where( oB => oB.IsDeleted == false || oB.IsDeleted == true );
    else
        myObjects = myObjects.Where( oB.IsDeleted == false );
    
    // Execute the query and return it to the caller    
    return myObjects.ToList();                    
}

Open in new window

0
 

Assisted Solution

by:ericlockshine
ericlockshine earned 0 total points
Comment Utility
One change, and its in the else:
    if( includeDeletedObjectB )
        myObjects = myObjects.Where( oB => oB.IsDeleted == false || oB.IsDeleted == true );
    else
        myObjects = myObjects.Where( oB => oB.IsDeleted == false );

Open in new window


This works great.  Thanks for the assist.
0
 

Author Closing Comment

by:ericlockshine
Comment Utility
Just the one bug, but thanks for the code.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now