locked
column using linq query RRS feed

  • Question

  • User-1261921818 posted

    Hi

    i have table with 10 columns but ii need 3 columns i want to write linq query, please help me

    for example i have table employee with field employeename, age,salary, name1, name2 etch...

    i want to select only employeename, age..

    Tuesday, June 30, 2015 6:00 AM

Answers

  • User1577371250 posted

    Hi,

    Here am using Anonymous Types

    var query = (from p in context.Employees
                select new { EmployeeName = p.employeename, Age = p.Age});


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 30, 2015 6:04 AM
  • User61956409 posted

    Hi desikankannan,

    As for your problem, I agree with Lokesh B R. The following sample is for your reference.

    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    
     protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Columns.Add("col3");
            dt.Columns.Add("col4");
            dt.Columns.Add("col5");
            dt.Columns.Add("col6");
            dt.Columns.Add("col7");
            dt.Columns.Add("col8");
            dt.Columns.Add("col9");
            dt.Columns.Add("col10");
    
            dt.Rows.Add("c11", "c12", "c13", "c14", "c15", "c16", "c17", "c18", "c19", "c10");
            dt.Rows.Add("c21", "c22", "c23", "c24", "c25", "c26", "c27", "c28", "c29", "c20");
    
            var records = from u in dt.AsEnumerable()
                            select new employee 
                            {
                                employeename = u.Field<string>("col1"),
                                age = u.Field<string>("col2"),
                                salary = u.Field<string>("col3")
                            };
    
            GridView1.DataSource = records;
            GridView1.DataBind();
        }
    }
    
    public class employee 
    {
        public string employeename { get; set; }
    
        public string age { get; set; }
    
        public string salary { get; set; }
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2015 3:27 AM

All replies

  • User1577371250 posted

    Hi,

    Here am using Anonymous Types

    var query = (from p in context.Employees
                select new { EmployeeName = p.employeename, Age = p.Age});


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, June 30, 2015 6:04 AM
  • User61956409 posted

    Hi desikankannan,

    As for your problem, I agree with Lokesh B R. The following sample is for your reference.

    <div>
        <asp:GridView ID="GridView1" runat="server"></asp:GridView>
    </div>
    
     protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Columns.Add("col3");
            dt.Columns.Add("col4");
            dt.Columns.Add("col5");
            dt.Columns.Add("col6");
            dt.Columns.Add("col7");
            dt.Columns.Add("col8");
            dt.Columns.Add("col9");
            dt.Columns.Add("col10");
    
            dt.Rows.Add("c11", "c12", "c13", "c14", "c15", "c16", "c17", "c18", "c19", "c10");
            dt.Rows.Add("c21", "c22", "c23", "c24", "c25", "c26", "c27", "c28", "c29", "c20");
    
            var records = from u in dt.AsEnumerable()
                            select new employee 
                            {
                                employeename = u.Field<string>("col1"),
                                age = u.Field<string>("col2"),
                                salary = u.Field<string>("col3")
                            };
    
            GridView1.DataSource = records;
            GridView1.DataBind();
        }
    }
    
    public class employee 
    {
        public string employeename { get; set; }
    
        public string age { get; set; }
    
        public string salary { get; set; }
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 1, 2015 3:27 AM