Solved

Using EF and included tables

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LINQ Select all records by MAX data syntax help 1 27
IDE for Python 5 63
Data organization issue 7 36
Converting PHP triple des decrypt to c# 9 62
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

735 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