Tag: linq

IEnumerable为DataTable性能问题

我有以下扩展,它从IEnumerable生成一个DataTable : public static DataTable AsDataTable(this IEnumerable enumerable) { DataTable table = new DataTable(); T first = enumerable.FirstOrDefault(); if (first == null) return table; PropertyInfo[] properties = first.GetType().GetProperties(); foreach (PropertyInfo pi in properties) table.Columns.Add(pi.Name, pi.PropertyType); foreach (T t in enumerable) { DataRow row = table.NewRow(); foreach (PropertyInfo pi in properties) row[pi.Name] = t.GetType().InvokeMember(pi.Name, BindingFlags.GetProperty, null, […]

无法访问具有group by的字段和使用linq的多个连接

我想只列出那些平均值与记录不同的类别。 这种差异存在于2个表中: TestOperation,TestOperationDifference 我想计算以下3个字段的平均值: TestOperation:DiffPerc If DiffPerc < 100 "Difference is there and take value of DiffPerc to calculate average" else "Dont take that record" TestOperationDifference:DiffPerc,DiffRec If DiffPerc < 100 "Difference is there and take value of DiffPerc and DiffRec to calculate average" else "Dont take that record" finalAverage=( Average(TestOperation.DiffPerc) + Average(TestOperationDifference.DiffPerc) + Average(TestOperationDifference.DiffRec) )/3 […]

Linq存储过程问题 – 返回int

我试图用Linq调用一个存储过程,存储过程在SQL中返回值很好,但当我将它拖到我的DBML文件并尝试从我的代码调用它时它返回 无法找到源类型“int”的查询模式的实现。 找不到“选择”。 我查看了其他线程和其他存储过程,由于某些原因而不是使用ISingleResult这是不同的,我似乎也无法更改返回类型。 这是背后的DBML代码 [global::System.Data.Linq.Mapping.FunctionAttribute(Name=”dbo.displayDetails”)] public int displayDetails([global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”VarChar(1)”)] string sex, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable day, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable month, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable year, [global::System.Data.Linq.Mapping.ParameterAttribute(Name=”PostCode”, DbType=”VarChar(10)”)] string postCode, [global::System.Data.Linq.Mapping.ParameterAttribute(Name=”AppTime”, DbType=”DateTime”)] System.Nullable appTime, [global::System.Data.Linq.Mapping.ParameterAttribute(DbType=”Int”)] System.Nullable filter) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), sex, day, month, year, postCode, appTime, filter); return ((int)(result.ReturnValue)); } 我的页面代码正在收到错误 var person = from p in db.displayDetails(sex.ToString(), […]

动态LINQ,选择函数,适用于Enumerable,但不适用于Queryable

我一直在摆弄动态LINQ一段时间了,但我还没有了解它的秘密。 我有一个我要解析的表达式,如下所示: “document.LineItems.Select(i => i.Credit).Sum();” 在解析这个问题时,我需要在LineItems Collection上调用Select函数。 我正在使用Expression.Call的工厂方法: Expression.Call( typeof(Queryable), “Select”, new Type[] { typeof(LineItem), typeof(decimal?) }, expr, Expression.Lambda(expression, new ParameterExpression[] { Expression.Parameter(typeof(LineItem) })); 此刻 expr: document.LineItems [ICollection] expression: LineItem.Credit [decimal?] 这些都没有实现。 我现在正在构建表达式树。 现在,问题是: 这个Expresssion.Call抛出和exception:“没有generics方法’选择’类型’System.Linq.Queryable’与提供的类型参数和参数兼容”; 我通过改变Expression.Call的第一个参数,通过在’System.Linq.Enumerable’而不是’Queryable’中查找’Select’来轻松解决它。 但是,这并不是我想要的。 我不希望所有LineItem都只计算Sum(),这显然是Enumerable的情况。 我想要Queryable工作。 另外,对于解析的最后一部分 – Sum(),我还需要使用Enumerable Sum(),因为Queryable Sum()会抛出相同的Exception。 我已经检查了MSDN,“选择”function的两个签名是相同的,所以我真的不明白为什么一个工作而另一个不工作。 任何帮助或指针都会受到重视。 问候,

合并从不同表中投射到一个实体的两个iqueryable

我试过这个答案 , 这一个和这个合并两个iqueryables。 但我总是收到以下错误: “Estudio”类型出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中。 可以在同一查询中的两个位置初始化类型,但前提是在两个位置都设置了相同的属性,并且这些属性以相同的顺序设置。 我正在从两个不同但相似的entity framework实体(EXAMEN和EXPLORACION)映射到我的域实体Estudio,具有以下代码。 IQueryable listExamen = context.Set().Project().To(); IQueryable listExploracion = context.Set().Project().To(); var listCombined = listExamen.Concat(listExploracion); 无论如何使用两个列表的合并生成IQueryable(不可枚举)? 如果使用AsEnumerable() ,则在内存上执行以下filter(Order,Take等)。 所以我需要合并列表,但仍然能够将filter应用于合并列表而不执行查询。 //This will force the next condition is executed on memory var listCombined = listExamen.AsEnumerable().Concat(listExploracion); 那可能吗?

inheritanceLinq to SQL类并转换linq查询的结果

我正在编写一个应用程序,我们需要将基本实体扩展为许多不同的东西(例如员工,车辆等)。 设计是这样的,即存在实体表和具有类型特定值的第二表,例如,雇员将具有ID号但车辆将具有注册号。 我从数据上下文中生成的类实体inheritance,但在我的存储库中进行转换时遇到问题。 这样做的正确方法是什么? public class cAccountEmployee : cAccountEntity { public string id_number { get { try { return this.cAccountEntityValues.Single(e => e.type == 1).value; } catch (Exception) { return “”; } } set { try { this.cAccountEntityValues.Single(e => e.type == 1).value = value; } catch (Exception) { this.cAccountEntityValues.Add(new cAccountEntityValue() { accountentity_id = this.id, cAccountEntity = […]

C#Linq对XML进行添加,更新,删除

我有这个代码 XElement newClient= new XElement(“Client”, new XElement(“Name”, cmbClient.Text), new XElement(“Service”, new XElement(“ServName”, cmbService.Text))); xmlDoc.Add(newClient); xmlDoc.Save(“Settings.xml”); 这创造了这个 Client Name Service Name 如果我再次按下Button1,那么它将创建另一个客户端部分,这没关系,但我想要的是: 如果不存在,请创建新的客户端部分。 如果Client存在,则向其添加 ServName,而不是替换已有的ServName。 如果客户端上已存在服务,则不执行任何操作,因为已存在。 任何线索? 我从linq开始到xml …感谢您的建议! 编辑:解决方案由mixin Dmitry Dovgopoly和Leon Newswanger提供答案谢谢你们两位! :d XDocument xDoc = XDocument.Load(“Settings.xml”); var Clients = from client in xDoc.Root.Elements(“Client”) where client.Element(“Name”).Value == cmbClient.Text select client; if (Clients.Count() > 0) { […]

在Linq查询中使用动态列名称

foreach (Dimension dimensions in Enum.GetValues(typeof(Dimension))) { var r = new ReferenceTable(dimensions).referenceItems; List qry = TVRawDataList.Where(p => !r.Any(d => d.Value == p.BrandVariant)) .ToList(); DimensionItem di = new DimensionItem(qry, dimensions); newDimensions.Add(di); } 我正在尝试创建一个Linq查询,将TVRawDataRecords列表与TVRawDataRecords枚举中的列表进行比较,如果没有匹配,则将它们添加到新的DimensionIem列表中。 这一切都运行正常,但我需要动态p.BrandVariant我的Where语句中的p.BrandVariant和维度枚举值,因为维度值与TVRawDataRecord属性名称相同。 这意味着我可以只用这几行代码来循环8个维度等。 有人可以解释我在Where语句中包含维度的方式吗? 谢谢!

LINQ和参考列表问题

我有以下代码与LINQ var q = (from web in DataContext.Webs select web); List k1 = new List() { 1, 2 }; List k2=new List() { 16, 17 }; q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId))); System.Diagnostics.Debug.WriteLine(q.Count()); k1 = k2; q = q.Where(web => DataContext.WebTechMaps.Any(t => t.WebsiteId == web.WebsiteId && k1.Contains(t.TechId))); System.Diagnostics.Debug.WriteLine(q.Count()); 这是它生成的查询。 第一次执行计数。 […]

条件Linq在嵌套对象上选择

鉴于我有一个像这样的简单对象 public class TestA { public int TestAId { get; set; } public string Description { get; set; } public IEnumerable TestBCollection { get; set; } } public class TestB { public int TestBId { get; set; } public int FkTestAId { get; set; } public string Description { get; set; } } List a […]