Solved

Using EF and included tables

Posted on 2013-05-31
3
235 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
ID: 39212264
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
ID: 39216122
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
ID: 39231315
Just the one bug, but thanks for the code.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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