Extension of an existing (anonymous) type in the selected request

While working with LinqPad I have a query Select x, Extra=f(y)where I would like to return all the properties (and field) xat the same level as Extra, rather than as separate xand Extraproperties (or fields).

Can this be done?

those. I want Select x.p1, x.p2, Extra=f(y)without gaining so much.

Note that the type xmay or may not be anonymous, just somewhat opaque, or too large for manual copying. The type caused by the implicit typing of VB.NET and the explicit C # core new {}is anonymous.

0
source share
1 answer

( , ).

My Extensions, , , , Select.

// Properties so you can "extend" anonymous types
public static string AllProperties<T>(this T obj, string VarName)
{
    var ps=typeof(T).GetProperties();
    return ps.Any()?(VarName + "." + string.Join(", " + VarName + ".", from p in ps select p.Name)):"";
}

// Fields so you can "extend" anonymous types
public static string AllFields<T>(this T obj, string VarName)
{
    var fs=typeof(T).GetFields();
    return fs.Any()?(VarName + "." + string.Join(", " + VarName + ".", from f in fs select f.Name)):"";
}

Select x.AllProperties("x") Take 1, Linq-to-SQL, : (from ... Select x).First().AllProperties("x"), , , ( , , LinqPad).

"x.p1, x.p2", โ€‹โ€‹ Select.

0

Source: https://habr.com/ru/post/1702468/


All Articles