Solved

IQueryable help

Posted on 2013-01-22
3
200 Views
Last Modified: 2013-01-23
I have a Gridview that im using a model entity to load using Iqueryable.

Trouble is Im not sure how to combine my second table from it.  In terms of the database its a streets table that acts as a master streets table and a benchmarks table that has many streets.  So the streetid is stored in the benchmarks table.  Now I did not use any virtual foreign keys here in my model so its just two seperate tables with nothing relating them other than streetid/id.

In the past id just create a sql command and join in the sql from one table to another.  Im not sure how to do this using linq.  If you look at the Iqueryable GetBenchMarks Method im returning an iqueryable based on the benchmarks table if i find a value in the querystring it means they are filtering so in that case im adding a where clause.  What I would like to do is join to the streets table on streetid and get the streetname from the streets table but im not sure how in linq.  

I will provide as much info here as I possibly can.

Sql tables.
sql tables
Gridview
gridview
GetBenchMarks Method
 //for gridview
        public IQueryable<Benchmark> GetBenchMarks([QueryString("hydrant")] string hydrant, [QueryString("revdate")] string revdate, [QueryString("street")] string street, [QueryString("quadrant")] string quadrant, [QueryString("desc")] string desc)
        {
            IQueryable<Benchmark> query = _db.Benchmarks;
           
           if (!String.IsNullOrWhiteSpace(hydrant))
            
                query = query.Where(p => p.HydrantNumber.Contains(hydrant));
           

            //month
            if (!String.IsNullOrWhiteSpace(revdate))
          
                query = query.Where(p => p.RevisionDate.Equals(revdate));

            //street


            if (!String.IsNullOrWhiteSpace(street))
          
                query = query.Where(p => p.Street1Number.Equals(street) || p.Street2Number.Equals(street));
         
            //quadrant
            if (!String.IsNullOrWhiteSpace(quadrant))
       
                query = query.Where(p => p.Quadrant.Equals(quadrant));
       

            //desc

            if (!String.IsNullOrWhiteSpace(desc))
         
                query = query.Where(p => p.Description.Contains(desc));

            
            return query;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //hydrant search

            string qstring = "";
            

            if (txtHydrant.Text.Length > 0)

               
                    qstring = qstring + "?" + "hydrant=" +  Server.UrlEncode(this.txtHydrant.Text);
               

            //Street search
            if (this.ddlStreet.SelectedItem.Text.Length > 0)
                if (qstring != "")
                {
                    qstring = qstring + "&" + "street=" + Server.UrlEncode(this.ddlStreet.SelectedItem.Text);
                }
                else
                {

                    qstring = qstring + "?" + "street=" + Server.UrlEncode(this.ddlStreet.SelectedItem.Text);
                }
                
                
                //qstring = qstring + "&" + "street=" + this.txtRevisionYear.Text;
               // Response.Redirect("BenchMarks_Home.aspx?street=" + this.txtStreet1.Text);
           

            //Quadrant search
            if (ddlQuadrant.SelectedItem.Text.Length > 0)

                if (qstring != "")
                {
                    qstring = qstring + "&" + "quadrant=" + Server.UrlEncode(this.ddlQuadrant.SelectedItem.Text);
                }
                else
                {

                    qstring = qstring + "?" + "quadrant=" + Server.UrlEncode(this.ddlQuadrant.SelectedItem.Text);
                }
                
                //qstring = qstring + "&" + "quadrant=" + this.txtQuadrant.Text;
                //Response.Redirect("BenchMarks_Home.aspx?quadrant=" + this.txtQuadrant.Text);
         

            //Description search
            if (txtDesc.Text.Length > 0)

                if (qstring != "")
                {
                    qstring = qstring + "&" + "desc=" + Server.UrlEncode(this.txtDesc.Text);
                }
                else
                {

                    qstring = qstring + "?" + "desc=" + Server.UrlEncode(this.txtDesc.Text);
                }
                //qstring = qstring + "&" + "desc=" + this.txtQuadrant.Text;   
                // Response.Redirect("BenchMarks_Home.aspx?desc=" + this.txtDesc.Text);
          
             
             Response.Redirect("BenchMarks_Home.aspx" + qstring);

Open in new window


Model
namespace Benchmarks.Model
{
    public class Benchmark
    {
        [Key]
        public int Id { get; set; }
        
        //HydrantNumber
        [Required(ErrorMessage = "Hydrant Number is required")]
        [Column(TypeName = "varchar")]
        [StringLength(15)]
        public string HydrantNumber { get; set; }

        //Location
        [Required]
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        public string Location { get; set; }

        //Description
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        [Required]
        public string Description { get; set; }

        //Elevation
        [Column(TypeName = "decimal")]
        [Required]
        public decimal Elevation { get; set; }

        //Remark
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        [Required]
        public string Remark { get; set; }

        //Reference
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        [Required]
        public string Reference { get; set; }

       //Street1Id
        public int Street1Number { get; set; }
        
        //Street2Id
        public int Street2Number { get; set; }

        //Order Number
        [Column(TypeName = "varchar")]
        [StringLength(15)]
        [Required]
        public string OrderNumber { get; set; }

        //Quadrant
        [Column(TypeName = "varchar")]
        [StringLength(2)]
        [Required]
        public string Quadrant { get; set; }

        //RevisionDate
        [Column(TypeName = "varchar")]
        [StringLength(10)]
        [Required]
        public string RevisionDate { get; set; }


    }

    public class Street
    {
        [Key]
        public int Id { get; set; }
        
        //streetname
        [Column(TypeName = "varchar")]
        [StringLength(50)]
        public string StreetName { get; set; }

     

    }
}

Open in new window

0
Comment
Question by:rochestermn
[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
 

Author Comment

by:rochestermn
ID: 38807268
I tried this but its giving me grief about not casting anonymous types.

error
0
 
LVL 14

Accepted Solution

by:
binaryevo earned 500 total points
ID: 38809495
Doing this from a mobile phone so make sure and check the syntax:

var mylist = from a in tablea
                    from b in tableb
                    where a.field1 == b.field2
                    select new myOject
                    {
                         Name = a.name,
                         Address = b.address
                     };
0
 

Author Closing Comment

by:rochestermn
ID: 38810360
good enough
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

729 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