Solved

IQueryable help

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
dynamic menu in asp.net c# 11 62
Please explain "Multi-Tenant Services" 5 64
ASP.NET Core index.html  LOCALHOST page cannot be found 2 25
Turn on intranet settings 1 40
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

17 Experts available now in Live!

Get 1:1 Help Now