仅显示选定的列

时间:2013-11-28 05:45:26

标签: c# linq

假设我有一份代表员工表的员工名单。

  public class Employee
    {
        public string Name { get; set; }
        public string Department { get; set; }
        public string Function { get; set; }
        public decimal Salary { get; set; }
        public DateTime EntryDate { get; set; }



        public static List<Employee> GetEmployeesList()
        {
            return new List<Employee>() {
                    new Employee() { EntryDate = new DateTime(2011, 05, 01), Name = "Fons",  Department = "Finance", Function = "Trader", Salary = 6500 },
                    new Employee() { EntryDate = new DateTime(2013, 05, 02), Name = "Mary",  Department = "Finance", Function = "BusinessAnalyst", Salary = 2500 },
                    new Employee() { EntryDate = new DateTime(2012, 04, 03), Name = "Alex",  Department = "Finance", Function = "Trader", Salary = 2100 },
                    new Employee() { EntryDate = new DateTime(2013, 05, 04), Name = "Jim",   Department = "R&D", Function = "Trainer", Salary = 3300 },
                    new Employee() { EntryDate = new DateTime(2010, 06, 05), Name = "Ellen", Department = "Dev", Function = "Developer", Salary = 2500 },
                    new Employee() { EntryDate = new DateTime(2000, 09, 06), Name = "Mike",  Department = "Dev", Function = "Consultant", Salary = 5100 },
                    new Employee() { EntryDate = new DateTime(1999, 03, 07), Name = "Jack",  Department = "R&D", Function = "Developer", Salary = 6100 },
                    new Employee() { EntryDate = new DateTime(1989, 01, 08), Name = "Demy",  Department = "Dev", Function = "Consultant", Salary = 3300 }};
        }
    }

我希望能够只选择要显示的所需列。 有些像:

   public static List<Employee> SelectColumnsFromTable(List<Employee> employees, int[] selectedColumns)
    {
        // only select colums 1, 3 and 4            
    }

我已经看过可以使用SQL和GridView,但在我的情况下,结果将打印在控制台上。 是否可以使用C#和Linq?

3 个答案:

答案 0 :(得分:1)

根据我的理解,重要的是根据索引选择类的特定属性。如果用户提供了相关索引,则可以使用反射动态访问属性。关键点是Type.GetPropertiesPropertyInfo.GetValue。我整理了一个小sample来证明:

using System;
using System.Collections.Generic;
using System.Linq;

public class Employee
{
    public int Id {get; set;}
    public string FirstName { get; set;}
    public string LastName {get; set;}
}

public class Test
{
    private static string[] GetColumnValues(Employee emp, params int[] cols)
    {
        var props = emp.GetType().GetProperties();
        var values = new List<string>();
        foreach(var i in cols)
        {
            if (i >= 0 && i < props.Length)
            {
                object value = props[i].GetValue(emp, null);
                values.Add(value == null ? string.Empty : value.ToString());
            }
        }
        return values.ToArray();
    }
    public static void Main()
    {
        var emp = new Employee() { Id = 1, FirstName = "John", LastName = "Smith" };
        var values = GetColumnValues(emp, 0, 2);
        Console.WriteLine(string.Join("\t", values));
    }
}

请注意,按照索引引用属性可能不太确定,您稍后会更改实现。因此,通过酒店名称选择可能会更稳定。此外,列选择器函数GetColumnValues不返回Employees,而是返回值作为字符串数组,因此您可以在String.Join中使用它。您可以在Linq中使用该功能:

var rows = from x in listOfEmps select GetColumnValues(x, 0, 2);
foreach(var row in rows)
    Console.WriteLine(string.Join("\t", row));

答案 1 :(得分:0)

var items = (from i in v.db.DatabaseName
            orderby i.EmpID descending
            select new {i.Name, i.Function,i.Salary}).ToList();

答案 2 :(得分:0)

var list = dt.AsEnumerable()
             .Where(row => (int)row["demoid"] > 5)//your condition here
             .Select(row => new
              {
                 demoid = Convert.ToInt32(row["demoid"]),
                 demoname = row["demoname"] != null ? 
                            row["demoname"].ToString() : 
                            String.Empty
              }).ToList();

或者您可以定义类:

public class myClass
{
  public int demoid;
  public string demoname;
}

然后:

List<myClass> list = dt.AsEnumerable()
             .Where(row => (int)row["demoid"] > 5)
             .Select(row => new myClass
             {
                 demoid = Convert.ToInt32(row["demoid"]),
                 demoname = row["demoname"] != null ? 
                            row["demoname"].ToString() : 
                            String.Empty
             }).ToList<myClass>();

这是为列表选择特定值。但是,您可以使用IList<myClass> classcollection= new List<myClass>();,然后根据条件将特定列表添加到class1。

注意:这里的类集合可以包含多个列表,因为你想要列1,3,4