linq-to-objects

i4o vs. PLINQ

I have a question for anyone who has experience on i4o or PLINQ. I have a big object collection (about 400K ) needed to query. The logic is very simple and straightforward. For example, there has a collection of Person objects, I need to find the persons matched with same firstName, lastName, datebirth, or the first initial of FirstName/...

Best object structure for linq to object?

I have a program that stores data in objects in memory that you can think of as small db in memory. I would like to use LINQ to Objects to run some simple queries on the in memory objects. Is there a preferred structure that I should use for the in memory objects. Are there any good resources that I should read before I get to far into t...

What would this sql query (w count and group by) look like when translated to linq?

How would the following sql query look when translated to linq? SELECT myId, Count(myId) FROM MyTable GROUP BY myId I've tried the following: var q = from a in db.MyTable group a by a.Id into g let count = g.Count() select new { Count = Id, Key= g.Key }; but it raises an exception on enumeration indicating that there is n...

Flattening Linq Group query

I have a list of data like so: ID AddressPurpose Address ... 1 L 1 P 2 L 2 P 3 P 3 L 4 P 4 L 5 P 6 L I want to be able to filter the data so that for each unique number if there is a P row then it is returned else the L row is returned. So the data will look like this: ID AddressPurpose Address ... 1 P 2 P 3 P 4 P 5 P ...

Sorting a list using Lambda/Linq to objects

I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of objects. Ex: public class Employee { public string FirstName {set; get;} public string LastName {set; get;} public DateTime DOB {set; get;} } public void Sort(ref List<Employee> list, string sortBy, string sortDirection) { ...

Linq OrderBy against specific values

Is there a way in Linq to do an OrderBy against a set of values (strings in this case) without knowing the order of the values? Consider this data: A B A C B C D E And these variables: string firstPref, secondPref, thirdPref; When the values are set like so: firstPref = 'A'; secondPref = 'B'; thirdPref = 'C'; Is it possible to o...

Replace a collection item using Linq

How do I find and replace a property using Linq in this specific scenario below: public interface IPropertyBag { } public class PropertyBag : IPropertyBag { public Property[] Properties { get; set; } public Property this[string name] { get { return Properties.Where((e) => e.Name == name).Single(); } //TODO...

LINQ to Objects question

I am writing a method that is passed a List<AssetMovements> where AssetMovements looks something like public class AssetMovements { public string Description { get; set; } public List<DateRange> Movements { get; set; } } I want to be able to flatten out these objects into a list of all Movements regardless of Description and am tr...

Join 2 lists by order instead of condition in LINQ

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 [...

LINQ to Objects - Is not in?

I have a generic list of custom objects and would like to reduce that list to objects where a specific property value is not in a list of exclusions. I have tried the following: Private Sub LoadAddIns() // Get add-in templates Dim addIns = GetTemplates(TemplateTypes.AddIn) // Get the current document Dim sectionId As String = C...

Update in LINQ to Objects

How to do an update in LINQ to Objects. Trying convert SQL to Linq Quality ( TransactionID int, Quantity float, MaterialID int, ProductID int, ParameterID int, ParameterValue float, TotalTonnes float ) How to convert below SQL to linq: UPDATE Q1 ...

linq group by a start time to endtime on DateTime Column

Ok this is kinda a messy query and I am only having limited success with it. I have a list of a Foo class that has a datetime property and other data. I have a class/row for allmost every minute, with some missing and some entire days missing like a holiday or weekend. My goal is to group each day for all rows from a start time to an ...

Linq Joining IEnumerable(Of Struct) to IEnumerable(of object)

Hi, With class RoleRecord (Guid RoleId, string RoleName,...) I am trying to get a new list of Name where the RoleId matches a list of Guid IEnumerable<RoleRecord> roles; IEnumerable<Guid> roleIds; I was thinking about avoiding the nested for loops, and along the lines of : var query = from rowA in roles ...

C# Ranking of objects, multiple criteria

I am building a plugin for a LAN party website that I wrote that would allow the use of a Round Robin tournament. All is going well, but I have some questions about the most efficient way to rank over two criteria. Basically, I would like the following rakning layout: Rank Wins TotalScore PersonE 1 5 50 PersonD 2 ...

Searching for text in sql server stored procedures

Hi, I have created a program that allows me to search for keywords inside the code of stored procedures, functions, tables etc. in SQL Server generated text files. I am looking for a way to find out where a search term (i.e "ADMIN.TABLE) is being referenced in all the code files. I use LINQ to find references. For example when I search f...

Grouping - Linq to Objects

With my following code: using System.Collections.Generic; using System.Linq; namespace SampleGrouping { internal class Program { private static void Main(string[] args) { var sample = new List<Samples> { new Samples{ParameterID = 11,MaterialID = 8...

LINQ to Objects - Does Not Contain?

I have a collection of Items that each have a collection of Relationships. I have a list of Groups that Items can have Relationships with. I can find all the Items that have a particular relationship but I now want to find all the Items that don't have a Relationship with any of my Groups. I can find the Items that have a relationship...

LINQ to JSON

I am trying the execute the sample code of LINQ to JSON (mentioned below) but it is giving me following error Stack Trace: [InvalidOperationException: Lambda Parameter not in scope] Code I am executing is: JObject rss = new JObject( new JProperty("id", "James Newton-King"), new JPrope...

DataTable aggregation problem

I am trying to query a DataTable inorder to calculate the sum of 2 columns in it and grouping the result by the rest of the fields. The thing is I can only get the aggregated column values and I can't get the non aggregated values. Here's the code I am using: var balances = from b in dtAccounts.AsEnumerable() group b by...

Use a linq expression to get a single array or list of strings from inside a collection of objects without using foreach and addrange

public class Buddy { public string[] Sayings; } Buddy one = new Buddy(); one.Sayings = new[] {"cool", "wicked", "awesome"}; Buddy two = new Buddy(); two.Sayings = new[] {"bad", "lame", "boring"}; Buddy[] buddies = new[] {one, two}; IEnumerable<string[]> something = from b in buddies select b.Sayings; So basical...