linq

Retrieve All Children From Parent[]

Given this class: public class Parent { public Child[] Children {get;set;} } And this array: Parent[] parents; How can I retrieve all the children from the parents array using Linq or something else? This seems bad: IList<Child> children = new List<Child>(); foreach(var parent in parents) children.AddRange(parent.Children); ...

how can i explain this code?

in .net, it is possible to write: (from n in numbers where n == 5 select n).ToList(); without those brackets, it is not possible to call the ToList() method. how can i explain to someone what this line does (all i can say is it precompiles the query, but i do not know if this is actually 100% correct). ...

Does dynamic GroupBy work together with dynamic Where in dynamic LINQ ?

I use this dynamic LINQ library together with Linq-to-Entities. I build query and after that iterate it with foreach(object e in query){} query=db.Table1.Where("it.FieldA>10").Select("it.FieldB"); works. query=db.Table1.Where(e=>e.FieldA>10).GroupBy("it.FieldB", "it").Select("key") works. But query=db.Table1.Where("it.FieldA>10").Grou...

How to do a nested count with OData and LINQ?

Here is the query I am trying to run from my OData source: var query = from j in _auditService.AuditJobs.IncludeTotalCount() orderby j.Description select new { JobId = j.ID, Description = j.Description, SubscriberCount = j.JobRuns.Count() }; It runs great if I don't use the j.JobRuns.Count...

transform a lambda expression

Hi I have the following code Expression<Func<IPersistentAttributeInfo, bool>> expression = info => info.Owner== null; and want to tranform it to Expression<Func<PersistentAttributeInfo, bool>> expression = info => info.Owner== null; PersistentAttributeInfo is only known at runtime though Is it possible? ...

Using Generic with Func as a parameter

My code is simply: public override C Calculator<C>(Team[] teams, Func<Team, C> calculatorFunc) { return teams.Average(calculatorFunc); } I get this error: Error 2 The type arguments for method 'System.Linq.Enumerable.Average(System.Collections.Generic.IEnumerable, System.Func)' cannot be inferred from the u...

How to generate the below XML format in Linq XML.

Below are my table structures; using those tables I want to create XML file. CREATE TABLE [dbo].[Security_Module_Menu]( [Client_Company_ID] [smallint] NOT NULL, [Module_ID] [tinyint] NOT NULL,Module_ID,Menu_ID,Reference_Menu_ID [Menu_ID] [int] NOT NULL, [Reference_Menu_ID] [int] NULL, [Menu_Name] [nvarchar](50) NULL,...

How can I use current user (default membership) as a data fied in DetailsView?

Hi, I am new to asp.net. I have a details view control on my page. I use linq to sql. My details view is an admin panel for data entry page. So some members will enter some news to my database. I want to use "Writer" Data Field to be filled automaticly with the current logged user. How can I do that? Thanks ...

"Lazy" GroupBy with Linq

I have recently been in a situation where I needed to perform an operation a grouped slowly yielding Linq query. Now, groupBy looses it's lazyness, that means that you have to wait for the entire Sequence to finish until you get any groups returned. This to me logically seems not the best solution, as a group can be returned as soon as ...

When Where clause is used inside Linq statement produces different results than when used outside

I have the following statement: List<string> tracks = new List<string> { "ABC", "DEF" }; var items = (from i in Agenda.AgendaSessions select i).Where(p => p.Tracks.Any(s => tracks.Contains(s.Code))); this returns all sessions which track contains either ABC or DEF, now when I rewrite the statement like the following, it returns All ...

EqualityComparer in LINQ - how can I do?

I think that the code below is good. But how can I write it in LINQ? how can I compare Employee type object in linq? namespace GenericReplacement { class Program { static void Main(string[] args) { EmployeeComparer employeeComparer = new EmployeeComparer(); Employee employee1 = new Employ...

ArrayList Problem in LINQ

Hi, i have problem LINQ query.In above,i got error system.object cant be converted to Sytem.String. What can be the problem? if i use string() instead of ArrayList, it doesn't raise error. But in String(), i should add items manually Public Shared Function GetCompletionList(ByVal prefixText As String, ByVal count As Integer, ByVal con...

Expression Trees

I have a method which have this signture public static IList<T> GetBy<T>(System.Linq.Expressions.Expression<Func<T, bool>> expression) i use to pass lambda expressions and make search restriction in nhibernate by reteriving data from expressiontree so when class user pass something like : c => c.fullName == "John" && c.lastName == "...

how to check if object already exists in a list

I have a list List<MyObject> myList and i am adding items to a list and i want to check if that object is already in the list. so before i do this: myList.Add(nextObject); i want to see if nextObject is already in the list. the object "MyObject" has a number of properties but comparison is based on matching on two propertie...

GroupBy with multiple groups as a hierarchy

I am using GroupBy create a hierarchical set of groups to use in multiple child grids. Assume I have a query with with 6 columns, a, b, c, d, e, f. Now, I need to group by a, then by b, then by c. and return the entire row in the group of c's. var q = rows.GroupBy(x => x.a) Ok, that's nice. That gives me my group of a's. Next, we ...

Linq - Group by multiple tables.

Using Linq to Sql how do i group the following 2 tables. Orders Table: CustomerID | Name |Date 1 | order1 | 2010-01-01 2 | order2 | 2010-01-01 2 | order3 | 2010-04-01 Calls Table: CustomerID | Name |Date 1 | call1 | 2010-01-01 3 | call2 | 2010-06-01 2 ...

space complexity of a simple linq(to objects) query

I have; var maxVal = l.TakeWhile(x=>x < val).Where(x=>Matches(x)).Max(); How much space does this need ? Does linq build up a list of the above Where() condition, or is Max() just iterating through the IEnumerable keeping track of what is the current Max() ? And where can I find more info about this, besides asking on SO f ...

RegEx / Parsing Linq - List.Where(xyz).Sum(value)

I have a linq like syntax: MyListName.Where(Lots of stuff can() "be" in "Diff(" here).Sum(value) I am wondering whether to use a RegEx or my own parsing function? I need to return: List = MyListName and WhereCondition = Lots of stuff can() "be" in "Diff(" here Function = Sum FunctionParameter = value e.g. the where brackets must matc...

Linq: Grouping a list, sorting it and getting top x values?

I have a list: Name Country Value John Ireland 100 Mary UK 200 Peter Germany 300 Bob UK 100 Pat France 400 I need to Group the list by the country, then sort by the summed values DESC then take the top X values e.g. List.TopX(3) would return France 400 UK 300 Germany 300 ...

C#/Linq: Where X is Y?

Linq has this handy function Where that lets me filter the results of an enumerable... foreach (var method in typeof(Program).GetMethods()) { foreach (var attr in method.GetCustomAttributes(inherit: true).Where(a => a is UrlAttribute)) { Console.WriteLine(((UrlAttribute)attr).Url); } } But it doesn't seem very hand...