Tag: linq

设置匿名类型属性名称

假设我有以下代码: string SomeConst = “OtherName”; var persons = GetPersons(); //returns list of Person var q = persons.Select(p => new { SomeConst = p.Name }); 基本上我希望在q序列中使用名为OtherName而不是SomeConst的匿名类型。 我怎样才能实现这样的行为?

LINQ保证订购SelectMany?

我有一个有序的枚举数组IorderedEnumerable[] foo我想要展平它,以便foo的有序枚举按照它们存储在数组中的顺序连接在一起。 例如{{1,2,3},{4,5},{6}} => {1,2,3,4,5,6} 我可以通过IOrderedEnumerable bar = foo.SelectMany(x => x); 或者LINQ不能保证在展平时如何处理订单?

本机C#支持检查IEnumerable是否已排序?

是否有任何LINQ支持检查IEnumerable是否已排序? 我有一个我要validation的枚举是按非降序排序的,但我似乎无法在C#中找到它的本机支持。 我使用IComparables编写了自己的扩展方法: public static bool IsSorted(this IEnumerable collection) where T : IComparable { Contract.Requires(collection != null); using (var enumerator = collection.GetEnumerator()) { if (enumerator.MoveNext()) { var previous = enumerator.Current; while (enumerator.MoveNext()) { var current = enumerator.Current; if (previous.CompareTo(current) > 0) return false; previous = current; } } } return true; } 一个使用IComparer对象: public static […]

Linq将字符串转换为Dictionary

我正在使用字典来保存一些参数,我刚刚发现不可能序列化任何实现IDictionary的东西( 无法序列化IDictionary )。 作为一种解决方法,我想将字典转换为字符串以进行序列化,然后在需要时转换回字典。 因为我正在努力改进我的LINQ,这似乎是一个很好的地方,但我不知道如何开始。 这就是我在没有LINQ的情况下实现它的方法: /// /// Get / Set the extended properties of the FTPS processor /// /// Can’t serialize the Dictionary object so convert to a string (http://msdn.microsoft.com/en-us/library/ms950721.aspx) public Dictionary FtpsExtendedProperties { get { Dictionary dict = new Dictionary(); // Get the Key value pairs from the string string[] kvpArray = m_FtpsExtendedProperties.Split(‘|’); […]

C#LINQ计数元素与DISTINCT

我有一组KeyValuePair项目,其中DateTime为键,字符串为value。 基本上我的数据看起来像: 12/07/2013 – 10220 12/07/2013 – 10220 12/07/2013 – 12220 12/07/2013 – 11240 12/07/2013 – 15220 13/07/2013 – 23220 13/07/2013 – 35620 14/07/2013 – 15620 14/07/2013 – 15620 我想列出我每天有多少项(不同)。 所以查询会导致: 12/07/2013 – 4 13/07/2013 – 2 14/07/2013 – 1

如何将LINQ结果转换为DATATABLE?

有没有办法在不逐步遍历每个元素的情况下将LINQ表达式的结果转换为DataTable ?

如何在linq中写入不等于sql的运算符?

using (RapidWorkflowDataContext context = new RapidWorkflowDataContext()) { var query = from w in context.WorkflowInstances from c in context.Workflows where EmpWorkflowIDs.Contains((int)w.ID) && w.CurrentStateID != c.LastStateID select w; return query.ToList(); } 我有2个表:工作流和WorkflowInstances。 用于存储对象的工作流和用于存储实例的workflowInstances。 工作流表:ID,Name,FirstStateID,LastStateID WorkflowInstances表:ID,Name,WorkflowID,CurrentStateID 如何在linq中编写查询到sql,从WorkflowInstances中选择实例,其中CurrentStateID不等于LastStateID

使用Linq进行分段的XML字符串解析

假设我有一个支离破碎的XML,如下所示。 我可以使用XmlReader with Fragment选项来解析这个not complete XML字符串。 XmlReaderSettings settings = new XmlReaderSettings(); settings.ConformanceLevel = ConformanceLevel.Fragment; XmlReader reader; using (StringReader stringReader = new StringReader(inputXml)) { reader = XmlReader.Create(stringReader, settings); } XPathDocument xPathDoc = new XPathDocument(reader); XPathNavigator rootNode = xPathDoc.CreateNavigator(); XPathNodeIterator pipeUnits = rootNode.SelectChildren(“A”, string.Empty); while (pipeUnits.MoveNext()) 我可以使用Linq进行碎片化的XML字符串解析吗?

如何使用Linq对通用列表进行排序?

如何在linq中对myScriptCellsCount.MyCellsCharactersCount(list int type)进行排序 public class MyExcelSheetsCells { public List MyCellsCharactersCount { get; set; } public MyExcelSheetsCells() { MyCellsCharactersCount = new List(); } } void ArrangedDataList(DataTable dTable) { DAL.MyExcelSheets myexcelSheet = new DAL.MyExcelSheets(); myScriptCellsCount = new TestExceltoSql.DAL.MyExcelSheetsCells(); foreach (DataColumn col in dTable.Columns) myexcelSheet.MyColumnNames.Add(col.ColumnName.ToString()); foreach(DataColumn dc in dTable.Columns) foreach (DataRow dr in dTable.Rows) myScriptCellsCount.MyCellsCharactersCount.Add(dr[dc].ToString().Length); //How can i […]

如何在Linq查询中将DateTime转换为String?

我必须以MMM dd,YYYY格式显示日期。 var performancereviews = from pr in db.PerformanceReviews .Include(a => a.ReviewedByEmployee) select new PerformanceReviewsDTO { ReviewDate=pr.ReviewDate.ToString(“MMM dd,yyyy”), EmployeeName=pr.ReviewedByEmployee.Name, JobTitle=pr.ReviewedByEmployee.JobTitle, ReviewerComments=pr.CommentsByReviewer, EmployeeComments=pr.CommentsByEmployee }; 这是我得到的错误消息 ExceptionMessage:LINQ to Entities无法识别方法’System.String ToString(System.String)’方法,并且此方法无法转换为商店表达式。 ExceptionType:System.NotSupportedException 当我在pr.ReviewDate上应用ToString ,我得到错误。 请通过适当的解决方案指导我如何实现这一目标。 我知道在正常的C#编码中有几种选择,但在Linq我们怎么做。