我知道这个问题因使用的单词而变得奇怪。 但这是我正在学习LINQ组连接的MSDN页面上使用的术语,我将解释它们。 我正在尝试LINQ的数据是: class Product { public string Name { get; set; } public int CategoryID { get; set; } } class Category { public string Name { get; set; } public int ID { get; set; } } // Specify the first data source. static List categories = new List() { new Category(){Name=”Beverages”, ID=001}, […]
我有一个存储在b中的匿名类型列表(来自列表),我想在列表中添加一个新对象。 当我尝试将新的匿名对象添加到此列表时,我收到此错误: ‘System.Collections.Generic.List.Add(AnonymousType#1)’的最佳重载方法匹配具有一些无效参数。 我也得到这个错误:参数1:无法从’AnonymousType#2’转换为’AnonymousType#1’ var b = user.Orders.Select(i => new { id = i.BillingId , text = i.Billing.ToString() }).ToList(); b.Add(new { id = 0 , text = “New Billing Address” }); 提前致谢
在c#中, var x = new {}; 声明没有属性的匿名类型。 这有什么不同吗? var x = new object(); ?
为什么我不能将List转换为List ? 我有以下代码: var datasource = someList.Select(o => new { x = oA, y = oB }); dgvSomeGridView.DataSource = datasource.ToList(); dgvSomeGridView.DataBind(); 然后我使用以下代码访问GridView.DataSource : var ds = ((List)dgvSomeGridView.DataSource); …. 但它在我将其转换为List的行上引发错误,它说: 无法将类型为System.Collections.Generic.List’1[f__AnonymousType0’8[System.Int32,System.String]]对象System.Collections.Generic.List’1[f__AnonymousType0’8[System.Int32,System.String]]转换为System.Collections.Generic.List’1[System.Object] 。 为什么我不能将匿名类型列表转换为dynamic ,或者将错误描述为object类型? 我该如何解决这个问题? 我的代码是在C#,框架4.0,在VS2010 Pro中构建,平台是ASP.NET。 请提前帮助,谢谢。
当我尝试运行以下entity framework查询时: 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(); 我得到以下exception: 转换为值类型“DateTime”失败,因为实现值为null。 结果类型的generics参数或查询必须使用可空类型。 问题可能是Action.ActionDate在EF模型中定义为不可为空的DateTime ,但是当没有为Sample分配相关操作时,查询返回null 。 解决方法是返回具有可空属性的非匿名类型,但为什么匿名类型不能接受null结果? 是否可以以某种方式强制使用可空属性创建匿名类型?
注意:我的问题不是关于ASP.Net。 我有一个使用LINQ与匿名集合绑定的GridView。 我想在网格中的事件处理程序中从绑定对象获取一个值,该值无法转换为任何静态类型,因为它的类型是匿名的。 为了解决这个问题,我使用动态类型来获取值 protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { dynamic dataItem = e.Row.DataItem; // getting the bound object bool isCurrentEntity = dataItem.IsCurrentEntity; if (isCurrentEntity) e.Row.Style.Add(HtmlTextWriterStyle.FontWeight, “Bold”); } } 我的问题是,使用动态类型这是一个不好的做法?
假设我有一个匿名类实例 var foo = new { A = 1, B = 2}; 有没有快速生成NameValueCollection的方法? 我想实现与下面的代码相同的结果,而不事先知道匿名类型的属性。 NameValueCollection formFields = new NameValueCollection(); formFields[“A”] = 1; formFields[“B”] = 2;
我需要获取可以写入的匿名类型的所有属性。 例如: var person = new {Name = “Person’s Name”, Age = 25}; Type anonymousType = person.GetType(); var properties = anonymousType.GetProperties(BindingFlags.Public | BindingFlags.Instance); 问题是所有属性的CanWrite属性都为false 。 对于非匿名类型,返回true。 我也试过调用PropertyInfo.GetSetMethod() ,它返回null 。 如何检查属性是否可以写入? 编辑:也许知道一个类型是否是匿名的就足够了。 如何使用reflection查明某个类型是匿名的?
当您尝试编译时: var car = new { “toyota”, 5000 }; 您将收到编译器错误“无效的匿名类型成员声明符。必须使用成员分配,简单名称或成员访问声明匿名类型成员。” 因为编译器无法从相应的表达式推断属性的名称。 这完全有道理。 令我好奇的是,错误消息暗示了三种声明类型成员的有效方法。 成员分配和成员访问是显而易见的: // member assignment var v = new { Amount = 108, Message = “Hello” }; // member access var productQuery = from prod in products select new { prod.Color, prod.Price }; 用简单名称声明的例子是什么? 谷歌搜索和SO上的相关问题仅导致成员分配和成员访问的示例。
我在Medium Trust中对Linq查询进行自定义投影,我得到一个MethodAccessException或TypeAccessException抱怨reflection和安全权限。 我已将代码简化为以下内容: var anon1 = new { Name = “Bill Gates” }; var ctor = anon1.GetType().GetConstructors().First(); // With native Reflection it works var anon2 = ctor.Invoke(new object[] { “Steve Ballmer” }); var expr = Expression.New(ctor, Expression.Constant(“Scott Guthrie”)); var lamb = Expression.Lambda(expr); // This throws in Medium Trust var anon3 = lamb.Compile().DynamicInvoke(); anon1.ToString(); // –> […]