Solved

IQueryable help

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

26 Experts available now in Live!

Get 1:1 Help Now