Consider the following:
// select a subset of the DataTable var subset = DataTable.Where(...).Select(row => new { Id = Convert.ToInt32(row["Id"]), Name = row["Name"].ToString(), Email = row["Email"].ToString() }); // or create a new object var subset = new { Id = 1, Name = "something random", Email = "name@domain.tld" };
Is there a way to use a subset variable as a parameter for a method, without it being presented as simple Object? Can you somehow transfer the automatically generated type of a variable?
Object
I try to avoid having to create new classes every time I want to pass LINQ subsets to methods.
Welcome random generic approaches.
Here's what I came up with ... Anobject extension method:
public static class ObjectExtensions { /// <summary> /// Cast Object to anonymous type. /// E.G.: new Object().ToAnonymousType(new { Property = new Type() }); /// </summary> public static T ToAnonymousType<T>(this Object o, T t) { return (T)o; } }
Using:
public void HandleAnonymousTypeAsParameter(Object o) { var anonymousType = o.ToAnonymousType(new { Id = new Int32(), Foo = new String(), Bar = new String() }); // ... You can do this in even less characters: var anonymousType = o.ToAnonymousType(new { Id = 0, Foo = "", Bar = "" }); } HandleAnonymousTypeAsParameter(new { Id = 1, Foo = "foo", Bar = "bar" });
The loans are owned by John Skeet and Thomas P.
, , *. .
var subset = DataTable.Where(...).Select(row => new SomeType { Id = Convert.ToInt32(row["Id"]), Name = row["Name"].ToString(), Email = row["Email"].ToString() });
Tuple, .NET 4. "" .
* , , .
, " ". , API, - . (, ) , ..
, struct ( ).
struct
- - .. Action<int,string,string> callback - - :
Action<int,string,string> callback
foreach(item in query) callback(item);
, , :
DoSomething(args, (id, email, name) => Email(To: email, Subject: name));
( , (id, name, email), , )
(id, name, email)
:
public static void Foo<T>(T item) { // Do whatever }
,
Foo(subset);
T . , ... , . , Foo Id, Name, Email ..
T
Foo
Id
Name
Email
, , . - , , , , LINQ.
# 5, , , (, , -, ToString), . , ...
ToString
Anonymous types do not provide much help outside the context that they created.
If you need to pass an Anonymous type to a method, this method is very general (Example)
void PrintAllObjectProperties(object obj);
After all, you will use reflection to do the work, or you are doing something wrong.
Source: https://habr.com/ru/post/1759083/More articles:Django: Lower case input file name - djangoVS2010 + NUnit not loading assemblies? - c #How to do automatic data archiving in SQL Server? - sql-serverManaging ViewState in WebFormsMVP - asp.netCan I temporarily suspend SharePoint Server + IIS + SQL Server? - windows-7Point Properties in .net - c #Javascript lint error suppression - javascriptPlanning Question - c #how can I format a date or double values when saving objects using simple xml - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1759088/writing-packed-structs-to-file-using-c&usg=ALkJrhig6L5vprKROYahyvLVNGtXpRvDQwAll Articles