Questions tagged [linq-to-objects]

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML.

The term "LINQ to Objects" refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly, without the use of an intermediate LINQ provider or API such as LINQ to SQL or LINQ to XML.

For example, say that myCats is a list of Cat objects. Then you can pick your favorite cat by using:

Cat myFavorite = myCats.Single(c => c.Score == myCats.Max(x => x.Score));

To get an alphabetical listing of your cats, use this:

var listing = myCats.OrderBy(c => c.Name);

To get all cats that have caught more than 20 mice, use the following:

var champions = myCats.Where(c => c.MiceCaught.Count > 20);

You can also use a more SQL-like syntax:

var champions = from cat in myCats where cat.MiceCaught.Count > 20 select cat;

LINQ to Objects thus uses the same familiar syntax as the other LINQ implementations such as LINQ to Entities and LINQ to SQL.

1356 questions
-1
votes
1 answer

LINQ .NET 4.0 - WHERE alone OK - ORDERBY alone OK - WHERE and ORDERBY Fails

Below runs correctly without error: IEnumerable FieldDefQuery = from fds in FieldDefs where fds.DispLevel == enumDispLevel.Grid select fds; Below runs correctly without error: IEnumerable FieldDefQuery = …
paparazzo
  • 44,497
  • 23
  • 105
  • 176
-1
votes
2 answers

Filtering Object values in C#

i've got a C# web Api receiving the following object : "cars": { "bmw": true, "benz": false, "kia": true, "hyundai": false, "madza": false, "ford": false } the class property is as follows : public…
Silva
  • 625
  • 1
  • 6
  • 25
-1
votes
1 answer

Last() with arrays

How effective is using Last() for arrays? var array = new[] { ... }; var last = array.Last(); // or array[array.Length - 1] In sources it only distinguish IList, so is that true what Last() will enumerate a complete array to return last item?…
Sinatr
  • 20,892
  • 15
  • 90
  • 319
-1
votes
1 answer

Linq ToDictionary anonymous versus concrete types

I am trying to use Linq to convert a list to Dictionary. I have been able to get proper results with anonymous types as keys, but not with concrete types. Please see code snipped below: // Works. Produces 329 entries in dictionary, as…
Amit
  • 1,836
  • 15
  • 24
-1
votes
3 answers

How to GroupBy time-range (closed-times grouped together)

Assume we have this records: NID CId PushedAt 120 796 2015-09-04 18:00:53.6012627 +00:00 120 967 2015-09-04 18:00:51.9891748 +00:00 119 669 2015-09-04 17:45:56.8179094 +00:00 119 955 2015-09-04 17:45:55.2078154…
amiry jd
  • 27,021
  • 30
  • 116
  • 215
-1
votes
1 answer

LINQ Compare Two Genreic Same Type of List and Get Result of Which item added,removed and nochange

I have two Lists List previous=new List() { new Employee() { Id = 1, Name = "1" }, new Employee() { Id = 2, …
Nilesh Moradiya
  • 691
  • 1
  • 10
  • 19
-1
votes
1 answer

ORing LINQ Query in linq to object, built using expression trees

I want same field combine with ORs and between two diffferent fileds with AND. ex - If they have the same Field value, those all ORed. This allows us to say "State", "eq", "CA", OR "State", "eq", "TX" AND "FirstName", "eq", "John". (State==CA ||…
MDev
  • 41
  • 5
-1
votes
2 answers

Select distinct value with where clause for a datatable

I have a datatable. I am getting distinct value of column from below code. AllFields is my datatable. var distinctIds = AllFields.AsEnumerable() .Select(s => new { id = s.Field(ColumnName), }) .Distinct() …
Rohit
  • 69
  • 1
  • 7
-1
votes
2 answers

Method 'System.String GetState(int32)' has no supported translation to SQL

I'm using the below code to fetch the state. I'm getting the error" Method 'System.String GetState(int32)' has no supported translation to SQL".Please let me know where i'm doing a mistake. public IQueryable GetResult() { …
Naresh Reddy
  • 47
  • 1
  • 5
-1
votes
2 answers

Convert list of objects in object to csv

In vb.net How to convert a list of objects within another List of Objects to csv string. I tried below didn't work String.Join(",", Person.Cars.Select(Function(d) d.Colors.Select(Function(o) o.colorid.ToString))) Output should be colorid in string…
Gauls
  • 1,955
  • 6
  • 28
  • 44
-1
votes
1 answer

Parent navigation property is `null` when selecting child object

I have two classes that partake in a parent-child relationship. The parent class is Country, and the child class is City: public partial class Country { public Country() { this.Cities = new List(); } public int Id {…
Developerzzz
  • 1,123
  • 1
  • 11
  • 26
-1
votes
1 answer

Linq Filter Array of Delimited Strings

I have a string like this: RoleId,RoleName|CategoryId,CategoryName I split them first like this: string delm = "RoleId,RoleName|CategoryId,CategoryName"; string[] FieldsToReplace = attributes[0].IdsToReplaceWith.Split('|'); Suppose i have a…
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
-1
votes
2 answers

Linq query to array

This is currently my Linq query: result = From d In dc.Tracker Where d.Responsible.Value = "first last name" Order By d.Priority1, d.Deadline Select New With { _ Key .Title = d.Name, _ Key .Description = d.Priority1, _ …
StealthRT
  • 10,108
  • 40
  • 183
  • 342
-1
votes
2 answers

Top 10 Percentile by salary of results

I have a in-memory List of Job objects, were the Job object has a propery called Salary. How do I use LINQ or C# to filter this list to only contain the list of jobs with salaries in the top 10 or bottom 10 percentile? Here is my attempt, which…
Abe
  • 6,386
  • 12
  • 46
  • 75
-1
votes
2 answers

C# Function that converts a string to a dictionary

I have a function that converts a string to a dictionary using the code below. I need to add a 3rd delimiter that is essential a row or record delimiter. Currently "," delimits [attribute,value] and "|" delimits each pair. I can't figure out how…
GoBeavs
  • 499
  • 2
  • 10
  • 25