linq-to-objects

Multiple descendants types linq

久未见 提交于 2020-01-04 02:07:08
问题 I sometimes do this: XElement.Descendants("mynodename"); is there a way to do something like this" XElement.Descendants("mynodename or myothernodename"); 回答1: Not in one method call - but you can use: element.Descendants() .Where(x => x.Name.LocalName == "mynodename" || x.Name.LocalName == "myothernodename") 回答2: Or, XElement.Descendants("mynodename") .Union(XElement.Descendants("myothernodename")); Which would sort them by type, then in order of appearance... 来源: https://stackoverflow.com

Group By either column using C# LINQ

女生的网名这么多〃 提交于 2020-01-03 17:52:27
问题 I have a set of Data with columns such as below OffName,RO1,RO2,RO3 To explain further i use sample data as below: OffName RO1 RO2 RO3 A John Jack Rob B Earl John Carl C Rob Chris Kenny D Rodney Carl Jacob RO stands for Reporting Officer. Each Officer reports to upto 3 RO's.i need to make a report where i need to show a grouping by RO irrespective of the person is RO1 or RO2 or RO3 for the officer..John is RO1 for Officer A and RO2 for Officer B, so when grouped by RO under John i want both

get distinct rows from datatable using Linq (distinct with mulitiple columns)

谁说胖子不能爱 提交于 2020-01-03 12:29:12
问题 I am trying to distinct on multiple columns and get datarows from datatable. but getting error. Dim query As IEnumerable(Of DataRow) = (From row As DataRow In SourceTable.AsEnumerable() _ Select row.Field(Of String)("ColumnName1"), row.Field(Of String)("ColumnName2") ).Distinct() below error: Unable to cast object of type '<DistinctIterator>d__7a`1[System.String]' to type 'System.Collections.Generic.IEnumerable`1[System.Data.DataRow]'. I want another datatable with distinct row based on given

LINQ querying a Dictionary against a List

丶灬走出姿态 提交于 2020-01-03 11:01:36
问题 I have List<string> selectedOptions; Dictionary<string,string> masterList; masterList comprises of Keys, which are the superset for the values in selectedoptions . Now, I would like to extract all the Values for the intersecting Keys between selectedOptions and masterList . How would the LINQ query be framed? 回答1: IEnumerable<KeyValuePair<string,string>> results = dic.Join(keys, d => d.Key, x => x, (a, b) => a); or of course var results2 = keys.Select(k => new {key = k, value = dic[k]}); but

LINQ to return null if an array is empty

╄→尐↘猪︶ㄣ 提交于 2020-01-03 08:40:15
问题 public class Stuff { public int x; // ... other stuff } I have a IEnumerable<Stuff> and I want to build a int[] of all of the x properties of all the Stuff objects in the collection. I do: IEnumerable<Stuff> coll; // ... var data = coll.Select(s => s.x).ToArray(); What I want is a null array rather than a int[0] if the collection is empty. In other words, if !coll.Any() , then I want data = null . (My actual need is that coll is an intermediate result of a complex LINQ expression, and I would

Dynamic LINQ Queries

坚强是说给别人听的谎言 提交于 2020-01-02 06:53:31
问题 Is it possible to create Linq Queries at runtime. Using an xml rule which can be translated to a Linq Query. 回答1: Ultimately, yes; but it isn't simple and you'll need to either: learn the Expression API use the pre-rolled dynamic LINQ library (from the samples download) If you want to go the first option, then you need to create your own lambdas; imagine, for example, that you have something like (making things up here...): <Filters> <Add Prop="Foo">My filter value</Add> </Filters> Then you

Aggregate values until a limit is reached

给你一囗甜甜゛ 提交于 2020-01-02 05:18:09
问题 I need something similar to an AggregateWhile method. The standard System.Linq.Enumerable class doesn't provide it. Until now I've always been able to leverage the standard LINQ methods to solve every problem I've encountered. So I'd like to know if that's still possible in this case, or if I really do need to extend LINQ with a non-standard method. The hypothetical AggregateWhile method would iterate over a sequence and apply the accumulator. The aggregation would be complete once a

Re-evaluate LINQ query when ObservableCollection changes

风流意气都作罢 提交于 2020-01-02 05:05:51
问题 I have a common issue that I'd like to (hopefully) find a better solution for moving forward. I have an ObservableCollection containing a master list of data. In my client code I need to 'transform' the data into a new form for display to the user. I use a LINQ statement like: var newList = (from item in observable select new { FirstInitial = item.Name[0] }); I know it's pretty rudimentary but it is enough to demonstrate the problem. (Notice the projection, this is not a simple filter or

Linq to objects - select first object

女生的网名这么多〃 提交于 2020-01-02 01:18:04
问题 I know almost nothing about linq. I'm doing this: var apps = from app in Process.GetProcesses() where app.ProcessName.Contains( "MyAppName" ) && app.MainWindowHandle != IntPtr.Zero select app; Which gets me all the running processes which match that criteria. But I don't know how to get the first one. The examples I can find on the net seem to imply I have to do this var matchedApp = (from app in Process.GetProcesses() where app.ProcessName.Contains( "MyAppName" ) && app.MainWindowHandle !=

Join 2 lists by order instead of condition in LINQ

一个人想着一个人 提交于 2020-01-01 10:28:19
问题 How can I join 2 lists of equal lengths (to produce a 3rd list of equal length) where I do not want to specify a condition but simply rely on the order of items in the 2 lists. Eg how can I join: {1,2,3,4} with {5,6,7,8} to produce: {{1,5}, {2,6}, {3,7}, {4,8}} I have tried the following: from i in new []{1,2,3,4} from j in new []{5,6,7,8} select new { i, j } but this produces a cross join. When I use join, I always need to specify the "on". 回答1: You could use Select in the first list, use