Solved

Using EF and included tables

Posted on 2013-05-31
3
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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