anonymous-types

System.Text.Json serialization against an anonymous object

你说的曾经没有我的故事 提交于 2020-01-24 18:36:12
问题 I'm working on an ASP .Net Core 3.1 application, porting part of the code from another using 2.2. So far, I'd like to switch from the NewtonSoft JSON serialization library to the new one, System.Text.Json, but I have some trouble. Consider a function to serve a HTTP-GET service with this returning type: [HttpGet("myservice")] public async Task<ActionResult<object>> GetDataAsync( ... Then, the final part could be depicted as follows: var items = new List<IMyInterface>(); int totalCount = ...

Get back anonymous type [duplicate]

喜欢而已 提交于 2020-01-24 14:09:09
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Accessing C# Anonymous Type Objects Working with C# Anonymous Types I'm using linq to entities to get id and full name from a table with customers. public IQueryable RegresaClientesPorEmpresa(int id_emp) { var clientes = from c in context.clientes where c.IDEmpresa == id_emp select new { c.IDCliente, NomComp = c.Nombres +" "+ c.ApellidoP +" "+ c.ApellidoM }; return clientes; } The result is used as the

how to convert an instance of an anonymous type to a NameValueCollection

≯℡__Kan透↙ 提交于 2020-01-23 04:48:08
问题 Suppose I have an anonymous class instance var foo = new { A = 1, B = 2}; Is there a quick way to generate a NameValueCollection? I would like to achieve the same result as the code below, without knowing the anonymous type's properties in advance. NameValueCollection formFields = new NameValueCollection(); formFields["A"] = 1; formFields["B"] = 2; 回答1: var foo = new { A = 1, B = 2 }; NameValueCollection formFields = new NameValueCollection(); foo.GetType().GetProperties() .ToList() .ForEach

Can we have an anonymous struct as template argument?

我与影子孤独终老i 提交于 2020-01-21 06:29:38
问题 The title is pretty self-explanatory, but here's a simplified example: #include <cstdio> template <typename T> struct MyTemplate { T member; void printMemberSize() { printf("%i\n", sizeof(T)); } }; int main() { MyTemplate<struct { int a; int b; }> t; // <-- compiler doesn't like this t.printMemberSize(); return 0; } The compiler complains when I try to use an anonymous struct as a template argument. What's the best way to achieve something like this without having to have a separate, named

Is it possible to return IEnumerable of anonymous objects from DataContext.ExecuteQuery?

拥有回忆 提交于 2020-01-14 10:16:06
问题 I develop a reporting engine where reports are based on templates. Every template has string with SQL query and every report has specific values for SQL query parameters. To render a report I set parameters and call DataContext.ExecuteQuery method to get list of records. But to catch returned columns I have to know their names and have a class with corresponding properties. Is it possible somehow to return IEnumerable of anonymous objects from DataContext.ExecuteQuery and then determine their

How do I get values from SelectedItem in ComboBox with Linq and C# 3.5

五迷三道 提交于 2020-01-13 22:42:33
问题 I am really missing something with anonymous types, because I can't figure out what to do with the Combobox.SelectedItem property. Here's the code that populates the combobox, and it works just fine var stocks = from st in brdc.tb_dStocks join su in brdc.tb_rStockUsers on st.StockID equals su.StockID where su.UserID == userRec.UserID select new { st.StockID, su.StockUserID, st.Ticker }; cboStocks.ItemsSource = stocks; cboStocks.DisplayMemberPath = "Ticker"; Then, when someone selects an item

How do I get values from SelectedItem in ComboBox with Linq and C# 3.5

倖福魔咒の 提交于 2020-01-13 22:40:18
问题 I am really missing something with anonymous types, because I can't figure out what to do with the Combobox.SelectedItem property. Here's the code that populates the combobox, and it works just fine var stocks = from st in brdc.tb_dStocks join su in brdc.tb_rStockUsers on st.StockID equals su.StockID where su.UserID == userRec.UserID select new { st.StockID, su.StockUserID, st.Ticker }; cboStocks.ItemsSource = stocks; cboStocks.DisplayMemberPath = "Ticker"; Then, when someone selects an item

Why the anonymous type instance cannot accept null values returned by the entity framework query?

这一生的挚爱 提交于 2020-01-13 19:20:07
问题 When I try to run the following Entity Framework query: var l = (from s in db.Samples let action = db.Actions.Where(x => s.SampleID == x.SampleID && x.ActionTypeID == 1).FirstOrDefault() where s.SampleID == sampleID select new { SampleID = s.SampleID, SampleDate = action.ActionDate, }).ToList(); I get following exception: The cast to value type 'DateTime' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type. The

LINQ Group By to project into a non-anonymous type?

纵饮孤独 提交于 2020-01-13 19:08:35
问题 I have the following LINQ example: var colorDistribution = from product in ctx.Products group product by product.Color into productColors select new { Color = productColors.Key, Count = productColors.Count() }; All this works and makes perfect sense. What I'm trying to achieve is to group by into a strong type instead of anonymous type. For example I have a ProductColour class and I would like to Group into a List<ProductColour> Is this possible? Thank you 回答1: EDIT: Okay, I'd completely

LINQ Group By to project into a non-anonymous type?

痞子三分冷 提交于 2020-01-13 19:08:28
问题 I have the following LINQ example: var colorDistribution = from product in ctx.Products group product by product.Color into productColors select new { Color = productColors.Key, Count = productColors.Count() }; All this works and makes perfect sense. What I'm trying to achieve is to group by into a strong type instead of anonymous type. For example I have a ProductColour class and I would like to Group into a List<ProductColour> Is this possible? Thank you 回答1: EDIT: Okay, I'd completely