Solved

IQueryable help

Posted on 2013-01-22
3
198 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
  • 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

Industry Leaders: 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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

679 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