Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Search 2 entities

Posted on 2013-01-10
5
Medium Priority
?
122 Views
Last Modified: 2013-01-17
I have 2 related entities.

public class Projects
    {
        [Key]
        public int ProjectID { get; set; }
        public int VersionNo { get; set; }        

        [StringLength(30)]
        public string CreateUserName { get; set; }


        public virtual ICollection<ProjectDetails> ProjectDetails { get; set; }

    } //public class Projects 

Open in new window

And
public class ProjectDetails
    { 
        public int ProjectDetailsID { get; set; }

        [Key, Column(Order = 1)]
        public int ProjectID { get; set; }
        [Key, Column(Order = 2)]
        public int VersionNo { get; set; }
              
        [StringLength(50), Display(Name = "Name")]
        public string ProjectLead { get; set; }



        public Projects Projects { get; set; } 
    
    } //public class ProjectDetails

Open in new window


I want to return the project and details to a gridview and am using this
public List<Projects> SearchProjectWithDetails(string SearchText, string sortExpression)
        {            

            using (var db = new CERTContext())
            {               
                List<Projects> PList = new List<Projects>();
                //PList = db.Projects.Where(p => p.ProjectID == 19).ToList();   //This worked
                                
                //This also works
                var Projects = (from p in db.Projects
                                where p.ProjectID > 10
                                orderby p.VersionNo descending
                                select p).Take(3);


                foreach (var Project in Projects)
                {
                    PList.Add(Project);
                }//foreach (var Function in Functions)

                return PList;

            }// using (var db = new CERTContext())

        }//public Projects GetProject(int ProjectId)

Open in new window


It does work but I would like to be able to search/filter on the Project lead.

Sql would have been select Projects.ProjectId , ProjectDetails.ProjectLead From
Projects inner join ProjectDetails on Projects.ProjectId = ProjectDetails.ProjectId
 and Projects.VersionNo= ProjectDetails.VersionNo
Where Projects .ProjectID > 10 And  ProjectDetails.ProjectLead  like '%Eamon%'
Order by Projects.VersionNo desc.

Can someone please help with the correct way to do this
Thanks
Eamon
0
Comment
Question by:Eamon
  • 3
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38763306
Try:

using System.Data.Linq.SqlClient;

...

                var Projects = (from p in db.Projects
                                join d in db.ProjectDetails on p.VersionNo equals d.VersionNo
                                where p.ProjectID > 10 &&
                                      SqlMethods.Like(d.ProjectLead, "%Eamon%")
                                orderby p.VersionNo descending
                                select p).Take(3);

...

Open in new window

0
 
LVL 1

Author Comment

by:Eamon
ID: 38763410
The type or namespace name 'Linq' does not exist in the namespace 'System.Data'

so sqlMethos is not working.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38763812
Add a reference to System.Data.Linq.dll.
0
 
LVL 1

Author Comment

by:Eamon
ID: 38766278
Getting this error when I try to fill the list
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 38767114
I didn't realize this was L2E. Try using Contains instead:

....

                var Projects = (from p in db.Projects
                                join d in db.ProjectDetails on p.VersionNo equals d.VersionNo
                                where p.ProjectID > 10 &&
                                      d.ProjectLead.Contains("Eamon")
                                orderby p.VersionNo descending
                                select p).Take(3);

....

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

971 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