简体   繁体   中英

Iterate through data returned from var to get names and values of columns

I am using Linq to Entities in a method and returning specific columns of data from the method. I don't know what type I'll be returning so I rigged it to return a List. If someone knows a better way than the way I rigged it, PLEASE let me know. See the code below.

After returning this data, I then need to iterate through the columns to get the column name and the value. I can see the string that the object has in it and it's JSON. It has "{ Column1 : Value1, Column2 : Value2 }". How can I iterate through to not just get the value, but get the name too...without using reflection?

Here's an example of the method I'm calling off to:

    public static List<object> GetDataSource(string QueryName)
    {
        MyEntity myEntity = new MyEntity();
        switch (QueryName)
        {
            case "FirstQuery":
                var s = (from x in myEntity.TableName
                         select new
                         {
                             Column1 = x.FirstColumn,
                             Column2 = x.SecondColumn
                         }).ToList();

                return s.Cast<object>().ToList();
        }
        return null;
    }

Anonymous types are local scoped. You cannot use them outside the method they were defined in (besides resorting to a horrible grotty hack ). Why not just define some domain class that will represent your data in a strongly typed manner:

public class Table
{
    public string Column1 { get; set; }
    public string Column2 { get; set; }
}

public static IEnumerable<Table> GetDataSource(string QueryName)
{
    MyEntity myEntity = new MyEntity();
    switch (QueryName)
    {
        case "FirstQuery":
            return from x in myEntity.TableName
                   select new Table
                   {
                       Column1 = x.FirstColumn,
                       Column2 = x.SecondColumn
                   };
    }
    return Enumerable.Empty<Table>();
}

I managed to implement the way you needed. It worked for me:

public class Table{
public string Column1 { get; set; }
public int Column2 { get; set; }}

 public static IEnumerable<T> GetDataSource<T>()
    {
        MyEntity myEntity = new MyEntity();

        if (typeof(T) == typeof(Table))
        {
            IQueryable<Table> query = from x in myEntity.Table1
                   select new Table
                   {
                       Column1 = x.TheColumn1,
                       Column2 = x.TheColumn2
                   };

           return query.ToList().Cast<T>();
        }
        if (typeof(T) == typeof(Table2))
        {
            IQueryable<Table2> query = from x in myEntity.Table2
                   select new Table2
                   {
                       TestColumn1 = x.TheColumn123,
                       TestColumn2 = x.TheColumn321
                   };

            return query.ToList().Cast<T>();
        }

        return Enumerable.Empty<T>();
    }

I haven't used the QueryName parameter, but you can always add it if there is more then one data sources that can IEnumerable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM