?
Solved

IQueryable help

Posted on 2013-01-22
3
Medium Priority
?
202 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 1500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 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