Solved

How to do a join with Lync and Entity Framework v4.0

Posted on 2015-02-08
3
28 Views
Last Modified: 2016-06-23
I have a sql server database from which I have created an edmx file in my csharp project. The database has all the requisite foreign keys setup and I can see these as NavigationProperties in the XML view of the edmx. All tables have come through as entities correctly.

In SQL I can run a query as follows:

SELECT * FROM Card.CardRequest cr
  JOIN product.CompanyProductDesign cpd ON cpd.CompanyProductDesignID = cr.CompanyProductDesignID
  JOIN product.ProductDesignCOntent pdc ON cpd.CompanyProductDesignID = pdc.CompanyProductDesignID

which will return only the rows that have been matched from all tables (in my case 6 rows). However I am struggling to get the same result using EF and Lync.

I have got the following code in my c# program:

                using (PrepayEntities ent = new PrepayEntities())
                {
                    cardRequests = ent.CardRequest
                                 .Include("CompanyProductDesign.ProductDesignContent")
                                 .Where
                                    (
                                        cr =>
                                            cr.CRIFileCreated == true
                                            &&
                                            cr.CreationEmailSent == false
                                    )
                                .ToList();
                }

which I know includes an extra Where condition but in any case returns all the rows (in my case 6100 rows). I have tried every possibility from examples that I have found the Internet but just can't seem to crack it - I either get 6100 rows or none.

Please can someone point me in the right direction for what I am sure is a simple solution

Thanks
Phil
0
Comment
Question by:LionCalledAlbert
3 Comments
 
LVL 35

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 40597561
Your LINQ join query should look like:
var result = from cr in ent.CardRequest
join cpd in ent.CompanyProductDesign on cr.CompanyProductDesignID equals cpd.CompanyProductDesignID
join pdc in ent.ProductDesignCOntent on cpd.CompanyProductDesignID equals pdc.CompanyProductDesignID
select new
{
//add more fields as needed
cr.CRIFileCreated,
cr.CreationEmailSent
};

Open in new window


There are more examples using join in this link
0
 
LVL 62

Assisted Solution

by:Fernando Soto
Fernando Soto earned 250 total points
ID: 40597566
Hi Phil;

Try this code snippet to see if it gives you what you are looking for.

using (PrepayEntities ent = new PrepayEntities())
{
    cardRequests = ent.CardRequest.
                   Join( CompanyProductDesign, 
                         cr => cr.CompanyProductDesignID,
                         cpd => cpd.CompanyProductDesignID,
                         (cr, cpd) => new
                             {
                              cr = cr,
                              cpd = cpd
                             }
                   ).
                   Join( ProductDesignCOntent,
                         temp => temp.cpd.CompanyProductDesignID,
                         pdc => pdc.CompanyProductDesignID,
                         (temp, pdc) => new
                             {
                               cr = temp.cr,
                               cpd = temp.cpd,
                               pdc = pdc
                             }
                   ).ToList();
}                   

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export import database 4 44
Close form "before" open 3 28
Update data using formula 22 23
Join multiple pivot queries 2 8
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

862 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

29 Experts available now in Live!

Get 1:1 Help Now