Solved

Using EF and included tables

Posted on 2013-05-31
3
236 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 63

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 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