linq-to-objects

.Net Reflection - Calling unexisting methods

Lets say i call method M1 on class A using reflection. The method does not exist. Is there any way to put a handler on class A that says, "someone is trying to execute method M1"? Or Is it possible to add a method dinamically to a class? I want to add a method M1...Mn that always does MyStaticClass.DoAction("M1...Mn"); Something li...

How can I generate an Expression tree that queries an object with List<T> as a property?

Forgive my clumsy explanation, but I have a class that contains a List: public class Document { public int OwnerId { get; set; } public List<User> Users { get; set; } public Document() { } } public class User { public string UserName { get; set; } public string Department { get; ...

Please help me convert this C# 2.0 snippet to Linq.

This is not a homework ;) I need to both A) optimize the following code (between a TODO and a ~TODO) and B) convert it to [P]Linq. Better readability is desired. It might make sense to provide answers to A) and B) separately. Thanks! lock (Status.LockObj) { // TODO: find a better way to merge these dictionaries foreach (KeyValue...

Dynamic group by in linq to objects on Silverlight

How can i do dynamic group by in linq to objects in #siverlight client side? i want to do something like this: var x = "empt.Dept"; var groups = from emp in employees group emp by x into g select new { Dept = g.Key, Count = g.Count() }; I tryed also to use reflection and do something like employee.GetType().GetProperty("Dep...

How to get the shortest time and longest time(or timespan) from a list using LINQ?

hi all, I am trying to get the longest and shortest timespan in a list using LINQ. My code looks something like this List listofTimeSpans = new List(); adding the timespans to listofTimeSpans in a foreach loop. Please help me with this. Thank you. ...

modify a variable in a anonymous method

I want to modify a local variable in a function of extension method. See int myvar=0; MyList.Where( x => { if (condition) myvar += 1; return false; }); return myvar; Why that is not working? ...

Teaching coworkers LINQ...

I have set myself upon a journey to educate my coworkers (all have accepted my mission, even the boss). Every day I seem to find a piece of code that could have been less error prone if my coworkers knew more about the framework, better-know-framework (in courtesy of DNR ;)) is part two of my teaching process. First part is teaching my c...

Like query ing LINQ to Object

Hello All i have a US states list List<string> state // contain all 51 US states Now i have a string which contain some text like okl (it means Oklahoma for me). what i want i want 'like' query in List state and get Oklahoma state. ...

Call method immediately after object construction in LINQ query

I've got some objects which implement this interface: public interface IRow { void Fill(DataRow dr); } Usually when I select something out of db, I go: public IEnumerable<IRow> SelectSomeRows { DataTable table = GetTableFromDatabase(); foreach (DataRow dr in table.Rows) { IRow row = new MySQLRow(); // Disregard the MySQLR...

Does breaking chained Select()s in LINQ to objects hurt performance?

Take the following pseudo C# code: using System; using System.Data; using System.Linq; using System.Collections.Generic; public IEnumerable<IDataRecord> GetRecords(string sql) { // DB logic goes here } public IEnumerable<IEmployer> Employers() { string sql = "select EmployerID from employer"; var ids = GetRecords(sql).S...

LINQ to Object to DataSet

Can I do a LINQ Join from an List to a DataSet? Are there any caveats to doing this? ...

Use LINQ, to Sort and Filter items in a List<ReturnItem> collection, based on the values within a List<object> property which is part of the ReturnItem class

This is tricky to explain. We have a DataTable that contains a user configurable selection of columns, which are not known at compile time. Every column in the DataTable is of type String. We need to convert this DataTable into a strongly typed Collection of "ReturnItem" objects so that we can then sort and filter using LINQ for use in ...

linq NullReferenceException while checking for null reference

I have the following LINQ query: List<FileInputItem> inputList = GetInputList(); var results = from FileInputItem f in inputList where ( Path.GetDirectoryName(f.Folder).ToLower().Trim() == somePath || Path.GetDirectoryName(f.Folder).ToLower().Trim() == someOtherPath ) && f.Expressi...

ToList().Sort() in Linq query gives compilation error

I am trying to combine two SortedDictionaries, change the result to a List<KeyvaluePair<string,string>> and Sort() the result. The following statement throws an error: var combinedEntries = from p in leftDict.Union(rightDict).ToList().Sort(myComparer) select p; Error: Could not find an implementation of the query pattern for source ty...

How to extract List<int> from Dictionary<int, string>?

I have a method that takes a List<int>, which is a list of IDs. The source of my data is a Dictionary<int, string> where the integers are what I want a list of. Is there a better way to get this than the following code? var list = new List<int>(); foreach (var kvp in myDictionary) { list.Add(pair.Key); } ExecuteMyMethod(list); ...

how to get cartesian products between database and local sequences in linq?

I saw this similar question here but can't figure out how to use Contains in Cartesian product desired result situation: http://stackoverflow.com/questions/1712105/linq-to-sql-exception-local-sequence-cannot-be-used-in-linq-to-sql-implementatio Let's say I have following: var a = new [] { 1, 4, 7 }; var b = new [] { 2, 5, 8 }; var te...

Simple linq question: using linq to get an array of properties

Lets say we have a simple class public class Foo { public string FooName; } Now we want to do some simple work on it. public void SomeCallerMethod(List<Foo> listOfFoos) { string[] fooNames = listOfFoo. // What to do here? } If I even knew what method to call, I could probably find the rest of the peices. ...

How to detect if a LINQ enumeration is materialized?

Is there some way of detecting whether an enumerable built using LINQ (to Objects in this case) have been materialized or not? Other than trying to inspect the type of the underlying collection? Specifically, since enumerable.ToArray() will build a new array even if the underlying collection already is an array I'm looking for a way of ...

LINQ OrderBy: best search results at top of results list

Consider the need to search a list of Customer by both first and last names. The desire is to have the results list sorted by the Customer with the most matches in the search terms. FirstName LastName ---------- --------- Foo Laurie Bar Jackson Jackson Bro Laurie Foo Jackson Laurie ...

LINQ Query to filter DTO

I have an array...and I need to exclude all the items in this array of string from the masterList.customField as shown below string[] excludeItem = {"a","b","c"}; CustomDTO[] masterList = service.LoadMasterList(); masterList.Where(c=> masterList.customField NOT IN excludeItem How do I achieve the NOT IN part above? ...