linq相当于'select *'sql的泛型函数?

时间:2021-09-04 15:03:11

I've got a generic<> function that takes a linq query ('items') and enumerates through it adding additional properties. How can I select all the properties of the original 'item' rather than the item itself (as the code below does)?

我有一个泛型<>函数,它接受一个linq查询('items')并通过它枚举添加其他属性。如何选择原始“项目”的所有属性而不是项目本身(如下面的代码所示)?

So equivalent to the sql: select *, 'bar' as Foo from items

所以相当于sql:select *,'bar'作为项目的Foo

foreach (var item in items)
{
    var newItem = new {
        item, // I'd like just the properties here, not the 'item' object!
        Foo = "bar"
    };

    newItems.Add(newItem);
}

5 个解决方案

#1


5  

There's no easy way of doing what you're suggesting, as all types in C# are strong-typed, even the anonymous ones like you're using. However it's not impossible to pull it off. To do it you would have to utilize reflection and emit your own assembly in memory, adding a new module and type that contains the specific properties you want. It's possible to obtain a list of properties from your anonymous item using:

没有简单的方法来做你所建议的事情,因为C#中的所有类型都是强类型的,甚至是你正在使用的匿名类型。然而,将它拉下来并非不可能。要做到这一点,你必须利用反射并在内存中发出自己的程序集,添加一个包含所需特定属性的新模块和类型。可以使用以下方法从您的匿名项中获取属性列表:

foreach(PropertyInfo info in item.GetType().GetProperties())
    Console.WriteLine("{0} = {1}", info.Name, info.GetValue(item, null));

#2


3  

Shoot you wrote exactly what i was going to post. I was just getting some code ready :/

拍你准确写了我要发布的内容。我刚刚准备好了一些代码:/

Its a little convoluted but anyways:

它有点复杂但反正:

ClientCollection coll = new ClientCollection();
var results = coll.Select(c =>
{
    Dictionary<string, object> objlist = new Dictionary<string, object>();
    foreach (PropertyInfo pi in c.GetType().GetProperties())
    {
        objlist.Add(pi.Name, pi.GetValue(c, null));
    }
    return new { someproperty = 1, propertyValues = objlist };
});

#3


0  

from item in items
where someConditionOnItem
select
{
     propertyOne,
     propertyTwo
};

#4


0  

Ask the item to give them to you.

要求该项目给你。

Reflection is one way... however, since all the properties are known at compile time, each item could have a method that helps this query get what it needs.

反射是一种方式......但是,由于所有属性在编译时都是已知的,因此每个项目都可以有一个方法来帮助此查询获得所需的内容。

Here's some example method signatures:

这是一些示例方法签名:

public XElement ToXElement()
public IEnumerable ToPropertyEnumerable()
public Dictionary<string, object> ToNameValuePairs()

#5


0  

Suppose you have a collection of Department class:

假设你有一个Department类的集合:

   public int DepartmentId { get; set; }
   public string DepartmentName { get; set; }

Then use anonymous type like this:

然后使用这样的匿名类型:

        List<DepartMent> depList = new List<DepartMent>();
        depList.Add(new DepartMent { DepartmentId = 1, DepartmentName = "Finance" });
        depList.Add(new DepartMent { DepartmentId = 2, DepartmentName = "HR" });
        depList.Add(new DepartMent { DepartmentId = 3, DepartmentName = "IT" });
        depList.Add(new DepartMent { DepartmentId = 4, DepartmentName = "Admin" });
        var result = from b in depList
                     select new {Id=b.DepartmentId,Damartment=b.DepartmentName,Foo="bar" };

#1


5  

There's no easy way of doing what you're suggesting, as all types in C# are strong-typed, even the anonymous ones like you're using. However it's not impossible to pull it off. To do it you would have to utilize reflection and emit your own assembly in memory, adding a new module and type that contains the specific properties you want. It's possible to obtain a list of properties from your anonymous item using:

没有简单的方法来做你所建议的事情,因为C#中的所有类型都是强类型的,甚至是你正在使用的匿名类型。然而,将它拉下来并非不可能。要做到这一点,你必须利用反射并在内存中发出自己的程序集,添加一个包含所需特定属性的新模块和类型。可以使用以下方法从您的匿名项中获取属性列表:

foreach(PropertyInfo info in item.GetType().GetProperties())
    Console.WriteLine("{0} = {1}", info.Name, info.GetValue(item, null));

#2


3  

Shoot you wrote exactly what i was going to post. I was just getting some code ready :/

拍你准确写了我要发布的内容。我刚刚准备好了一些代码:/

Its a little convoluted but anyways:

它有点复杂但反正:

ClientCollection coll = new ClientCollection();
var results = coll.Select(c =>
{
    Dictionary<string, object> objlist = new Dictionary<string, object>();
    foreach (PropertyInfo pi in c.GetType().GetProperties())
    {
        objlist.Add(pi.Name, pi.GetValue(c, null));
    }
    return new { someproperty = 1, propertyValues = objlist };
});

#3


0  

from item in items
where someConditionOnItem
select
{
     propertyOne,
     propertyTwo
};

#4


0  

Ask the item to give them to you.

要求该项目给你。

Reflection is one way... however, since all the properties are known at compile time, each item could have a method that helps this query get what it needs.

反射是一种方式......但是,由于所有属性在编译时都是已知的,因此每个项目都可以有一个方法来帮助此查询获得所需的内容。

Here's some example method signatures:

这是一些示例方法签名:

public XElement ToXElement()
public IEnumerable ToPropertyEnumerable()
public Dictionary<string, object> ToNameValuePairs()

#5


0  

Suppose you have a collection of Department class:

假设你有一个Department类的集合:

   public int DepartmentId { get; set; }
   public string DepartmentName { get; set; }

Then use anonymous type like this:

然后使用这样的匿名类型:

        List<DepartMent> depList = new List<DepartMent>();
        depList.Add(new DepartMent { DepartmentId = 1, DepartmentName = "Finance" });
        depList.Add(new DepartMent { DepartmentId = 2, DepartmentName = "HR" });
        depList.Add(new DepartMent { DepartmentId = 3, DepartmentName = "IT" });
        depList.Add(new DepartMent { DepartmentId = 4, DepartmentName = "Admin" });
        var result = from b in depList
                     select new {Id=b.DepartmentId,Damartment=b.DepartmentName,Foo="bar" };