linq-to-objects

Extracting a list of objects that exist in another list objects using linq

Hi All Linq noob here. I have IList<Product> ApplicableProducts and a IList<Product> CurrentProducts. I need to return a new IList<Product> of all CurrentProducts that exist in Applicable products. I understand I need to be working with .Contains and .Any but getting a little lost. Any tips appreciated ...

What's the best way to convert non-generic collection to a generic collection?

I've been teaching myself LINQ recently and applying it to various little puzzles. However, one of the problems I have run into is that LINQ-to-objects only works on generic collections. Is there a secret trick/ best practice for converting a non-generic collection to a generic collection? My current implementation copies the non-generi...

How to get the first element of IEnumerable

Is there a better way getting the first element of IEnumerable type of this: foreach (Image image in imgList) { picture.Width = (short)image.Columns; picture.Height = (short)image.Rows; break; } This is the exact declaration of the type: public class ImageList : IEnumerable, IDisposable ...

Methode Call twice inside LINQ Query

In our application we have a little query that looks like this: var selectedAgents = from agent in listAgents where (this.collectionVehicles.GetVehicleByAgent(agent)).IsActive || (this.collectionVehicles.GetVehicleByAgent(agent)).IsSoldSinceCurrentSession select agent; This works fine but the method GetVehic...

Linq casting between types

Is there a better way to cast this list of guid strings to guids using linq: public static IList<Guid> ToGuidList(this IList<string> guids) { IList<Guid> guidList = new List<Guid>(); foreach(var item in guids) { guidList.Add(new Guid(item)); } return guidList; } I looked at: ...

How to Get Font Properties from a Font File Name?

I'm trying to use Linq to loop through all fonts in the %windir%\Fonts folder and find the one that has a property title of "Arial" (or any Font Family name supplied), but I can't seem to access the font properties (things like "Title", "Font style", "Designed for", etc.). The following is only giving me the basic file info: Dim f...

LEFT OUTER JOIN in LINQ to objects

Consider the following code. City and CitPlace are joined by CityCode. What I want to do is perform a LEFT OUTER JOIN between CityPlace and City. City[] cities = new City[]{ new City{CityCode="0771",CityName="Raipur",CityPopulation="BIG"}, new City{CityCode="0751",CityName="Gwalior",CityPopulation="MEDIUM"}, new City{CityCode="0755"...

IEqualityComparer meaning

Consider this array string[] presidents = { "Adams", "Arthur", "Buchanan", "Bush", "Carter", "Cleveland", "Clinton", "Coolidge", "Eisenhower", "Fillmore", "Ford", "Garfield", "Grant", "Harding", "Harrison", "Hayes", "Hoover", "Jackson", "Jefferson", "Johnson", "Kennedy", "Lincoln...

LINQ to Objects Optimization Techniques?

What LINQ to Objects optimization techniques do you use or have you seen in the wild? While waiting for "yield foreach" and other language/compiler optimizations to arrive in C# in 201x, I'm interesting in doing everything possible to make using LINQ everywhere less of a performance pain. One pattern I've seen so far is creating custom...

Case insensitive string compare with linq-to-sql and linq-to-objects

see also Differences between LINQ to Objects and LINQ to SQL queries We are using the some queries over our database and our in memory objects. What is the best way of doing an insensitive string compare with linq-to-sql so that? It runs fast on SQL Server The same query expression can be used with linq-to-objects to get the same res...

Processing ranking data w/ C# & LINQ

I have an object in a list that I need to rank several different ways. Currently the code is rather unwieldy as it requires me to individually address each column. Example: public class Data { public int AValue { get; set; } public int ARanking { get; set; } public int BValue { get; set; } public int BRanking { get; set;...

Mux & Demux w/ LINQ

I'm playing around with using LINQ to Objects for multiplexing and demultiplexing but it seems to me that this is a pretty tricky problem. See this demuxer signature: public static IEnumerable<IEnumerable<TSource>> Demux<TSource>(this IEnumerable<TSource> source, int multiplexity) On an abstract level this is easy but ideally one wou...

Link To SQL Join Statement with or Clause

I have to following SQL Statement that I want to conver to LINQ Select F.FooName B.BarName From Foo F Inner Join Bar B on B.BarName = F.FooName OR B.BarName + 'hello' = F.FooName I have seen Inner joins in Link on multiple conditions with AND Clauses but not using OR The following is as far as I have gotten var myresult = from f ...

how to use "LIKE" in Linq to Object

I am using Linq to Object, now how can i use some thing like "LIKE" used in sql query to filter data? ...

Collection was modified exception linq-to-objects

I got a strange exception when trying to find out if a property in a list of objects is equal for all objects. This is my code: bool lvNoGuests = pvBillData.Reservering.Dagen.All(x => { return x.AantalKinderen == pvBillData.Reservering.Dagen[0].AantalKinderen && x.AantalVolwassenen == pvBillData.Reservering.Dagen[0].Aan...

return from a linq where statement

I have the following link function MyLinqToSQLTable.Where(x => x.objectID == paramObjectID).ToList(); I most of the time you can change a linq call to be several lines by adding curly brackets around the method body. Like this: MyLinqToSQLTable.Where(x => { x.objectID == paramObjectID; }).ToList(); Problem is the implied retu...

Is an object still connected to a list after FirstOrDefault?

Here's my code: Event thisEvent = (from i in list where (i.eventID == eventID) select i).FirstOrDefault(); if (thisEvent != null) { thisEvent.eventResolved = resolved; thisEvent.eventSequence.Add(item); } "list" is a collectio...

Use LINQ and C# to make a new List from an old List

This should be pretty simple, but I am new at LINQ. I have a List<FillStruct> of FillList structs. I'd like to use LINQ to create a new List<NewFillStruct> where instead of having the number of buys and sells, I would have one variable containing the sum. For example, if the FillStruct structure has buy = 4 and sell = 2 then t...

How to update an element with a List using LINQ and C#

I have a list of objects and I'd like to update a particular member variable within one of the objects. I understand LINQ is designed for query and not meant to update lists of immutable data. What would be the best way to accomplish this? I do not need to use LINQ for the solution if it is not most efficient. Would creating an Update ...

LINQ query code for complex merging of data.

I've posted this before, but I worded it poorly. I'm trying again with a more well thought out structure. I have the following code and I am trying to figure out the shorter linq expression to do it 'inline'. Please examine the "Run()" method near the bottom. I am attempting to understand how to join two dictionaries together based on a...