Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
i have two tables 1.city 2.training
1.city have the fields a. cityid(PK) b)city_name
2. training have the fields a. trainingid(pk) b)training_name c)cityid

i want to show grid view with the following fields.
1. training_name 2. city_name

please tell the linq query for asp.net that show the above fields.
Posted
Comments
What have you tried and where is the problem?

C#
var q = from n in dataContext.city
                    join m in dataContext.training on
                    n.cityid equals m.cityid
                    select new{
                    n.city_name,
                    m.training_name
                    };
GridView1.DataSource=q.ToList();
GridView1.DataBind();
 
Share this answer
 
v2
Comments
saifullahiit 1-Jan-14 0:31am    
it shows error type of one expression in join clause is incorrect. and second error is contextual keyword 'on ' is required
Vinay Jade 1-Jan-14 5:50am    
ok i will check
Vinay Jade 1-Jan-14 7:16am    
now its working fine plz check on your system and plz let me know
C#
using System.Collections.Generic;
using System.Linq;
class program
{
    static void Main(string[] args)
    {
        List<city> lstCity = new List<city>();
        List<training> lstTraining = new List<training>();
        lstCity.Add(new City() { cityid = 1, city_name = "tamilnadu" });
        lstCity.Add(new City() { cityid = 2, city_name = "karanataka" });
        lstTraining.Add(new Training() { training_name = "c#", cityid = 1 });
        lstTraining.Add(new Training() { training_name = "java", cityid = 1 });


       var data =  lstTraining.Join(lstCity, (k => k.cityid), (k => k.cityid), (t, c) => new { t, c }).Select(k => new
        {
            TraingName = k.t.training_name,
            CityName = k.c.city_name
        }).ToList();
    
    }
}

public class City
{
    public int cityid { get; set; }
    public string city_name { get; set; }
}
public class Training
{
    public int trainingid { get; set; }
    public string training_name { get; set; }
    public int cityid { get; set; }
     
}</training></training></city></city>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900