linq-to-objects

How do I order by and group by in a Linq to objects query?

How do I order by and group by in a Linq query? I tried.. Dim iPerson = From lqPersons In objPersons Where Len(lqPersons.Person) > 0 Group lqPersons By key = lqPersons.Name Into Group Order By Group descending Select Group, key For Each i In iPerson tmp = tmp & vbNewLine & i.key & ", " & i.Group.Count Next The above ...

Aggregation. Linq to objects query.

I have 3 classes public class Test { private List<Question> _questions; private string _text; public string Text { get { return _text; } } //... } public class Question { private List<Answer> _answers; private string _text; public string Text { get { ret...

Defining my own Where-Method for LINQ to Objects - How do I know which one will get used?

Hi everyone, Just for testing reasons, I defined my own Where-Method for Linq like so: namespace Test { public static class LinqTest { public static IEnumerable<TSource> Where<TSource>( this IEnumerable<TSource> source, Func<TSource, bool> predicate) { ...

Parallel.ForEach throws exception when processing extremely large sets of data

My question centers on some Parallel.ForEach code that used to work without fail, and now that our database has grown to 5 times as large, it breaks almost regularly. Parallel.ForEach<Stock_ListAllResult>( lbStockList.SelectedItems.Cast<Stock_ListAllResult>(), SelectedStock => { ComputeTipDown( SelectedStock.Symbol ); } ); The Com...

Writing a generic method for .Field<T>() in Linq to DataSet

I have been attempting to write a reusable generic method for lookups on a DataTable. What I have so far: private static IEnumerable<DataRow> GetRow<FType>(string Tablename, string Fieldname, FType Match) { var result = from row in dataSet.Tables[Tablename].AsEnumerable() where row.Field<FType>(Fieldname) == Ma...

how to send LINQ filter as parameter

Imagind I have the following in VB: function doSomething() From ou In ctxt.Users.Where(Function(p) p.UserName = username) ... end function how can I send the filter as parameter (something like below)? function doSomething(filter as whatTypeHereAndHowToInstantiateInCallingFunction) From ou In ctxt.Users.Where(filter) ...

Help in LINQ query with group by

Hi, I have the following List: 1: true; 2: false; 2: true; 3: false; 3: false; 3: false; I want a LINQ query to get a collection as the following: key, OR operation between the grouped items, for example: 2: false | true = true The result must be: 1: true 2: true 3: false Thanks in advance ...

How can do an invariant .Contains(myQuery) with Linq to Objects?

Hi folks, I'm trying to filter a list of objects using linq. When i filter by a Contains(someSearchQuery) it's like it's being case sensitive .. meaning i miss some results. I have an IList<Foo>'s which has a number of properties, but one is public string MyText { get; set; } Now, i'm trying to return a IQueryable of Foo's where the...

LINQ- Ordering a list of objects based on a particular value.

I am new to LINQ. I have a class like this: public class StudentResults { public int Id { get; set; } public ResultsStatus StatusResult { get; set; } public string Message { get; set; } public StudentDetails Detail { get; set; } } There is a method that returns a List of the above class to a variable I need to it...

comparing two objects by two properties

How to sort two objects in a list by using two properties one by ascending and the other by descending order. when linq is used it says i need to implement IComparer interface but not sure how to compare two objects by using two properties at once. Say Person class by Name ascending and Age descending. ...

Ienumerable Extension method for dictionary array calls

Hi All Im trying to get an enumberable collection from a dictionary held array. Or should I say, I'm trying to write an extension method for my dictionary objects which store arrays to return an IEnumerable item when the result is null. Im using dictionarys to store array datasets (there are speed reasons for this), which I extract at ...

Linq to Objects: filtering performance question

I was thinking about the way linq computes and it made me wonder: If I write var count = collection.Count(o => o.Category == 3); Will that perform any differently than: var count = collection.Where(o => o.Category == 3).Count(); After all, IEnumerable<T>.Where() will return IEnumerable<T> which doesn't implement Count property, so...

Using LINQ to query collections within collections in am MVC View

I have a "Product" and a "Benefit" class which is shown below: Now, I want to query a collection of products from within my view. I've created a LINQ query that gets the products but I want to get at the collection of benefits that belong within the product. Code within my view (however this just iterates each product and I need to ite...

How to access a particular data in LINQ query result ?

I know, this is very simple for you guys. Please consider the following code: string[] str = { "dataReader", "dataTable", "gridView", "textBox", "bool" }; var s = from n in str where n.StartsWith("data") select n; foreach (var x in s) { Consol...

Using LINQ, select list of objects inside another list of objects

public class ClassA { public string MyString {get; set;} } public class ClassB { public List<ClassA> MyObjects {get; set;} } List<ClassB> classBList = new List<ClassB>(); var results = (from i in classBList select i.MyObjects).ToDistinct(); I want a distinct list of all the ClassA objects in the classBList. How do I go abo...

LinqtoSQL with a where clause containing a List?

Hello All, I have researched this to death. There does seem to be some answers here already, but frankly the answers confuse me. So I'll try to make this as basic as possible. If I have a table in a database, "People" with a name column(string) and an age(int) column. Then I do something like this: List<int> ages = new List<int>();...

Problem implementing Join(...) extension method

I am trying to use the Join(...) extension method to build a query based on criteria passed to a method. I have an error in the following: public static IQueryable QueryItems(string param1, string param2, string param3) { IQueryable<tbl_Item> query = dataMap.tbl_ItemMap; //Join is giving me the error: Cannot implicitly convert ...

How can I filter a dictionary using LINQ and return it to a dictionary from the same type

Hi, I have the following dictionary: Dictionary<int,string> dic = new Dictionary<int,string>(); dic[1] = "A"; dic[2] = "B"; I want to filter the dictionary's items and reassign the result to the same variable: dic = dic.Where (p => p.Key == 1); How can I return the result as a dictionary from the same type [<int,string>] ? I trie...

Using LINQ to objects Intersect and Except on a specific property

When I have 2 List<string> objects, then I can use Intersect and Except on them directly to get an output IEnumerable<string>. That's simple enough, but what if I want the intersection/disjuction on something more complex? Example, trying to get a collection of ClassA objects which is the result of the intersect on ClassA object's AStr1...

C# Merging 2 dictionaries

I'm developing an app in C# targeting .NET 3.5. In it, I have 2 similar dictionaries that contain validation criteria for a specific set of elements in my app. Both dictionaries have identical signatures. The first dictionary has the default settings and the 2nd dictionary contains some user defined settings. var default_settings = ...