linq-to-objects

Select distinct values in all nested collections using LINQ to objects?

Given the following code setup: public class Foo { List<string> MyStrings { get; set; } } List<Foo> foos = GetListOfFoosFromSomewhere(); How do I get a list of all of the distinct strings in MyStrings across all of the Foo instances using LINQ? I feel like this should be easy, but can't quite figure it out. string[] distinctMyStrin...

Implementing Row-Level Security – (SPs vs LINQ to Objects)

Hi all, I believe this is more a question about best practices and design than anything else. I tried searching for similar queries regarding this but couldn’t find any. I actually found the Row Level Security with Entity Framework but I believe the context here is a bit different. I will try to explain my scenario first: I have a .ne...

Appropriate use of keyword "new" in LINQ

Please consider the following code : string[] words = { "Too", "much", "of", "anything", "is" ,"good","for","nothing"}; var groups =from word in words orderby word ascending group word by word.Length into lengthGroups orderby lengthGroups.Key descending select new {Length=lengthGroups.Key,...

How to get the Point with minimal X from an array of Points without using OrderBy?

Imagine I have var points = new Point[] { new Point(1, 2), new Point(2, 3) }; To get the point with the minimum X I could: var result = points.OrderBy(point => point.X).First(); But for large arrays, I don't think this is the faster option. There is a faster alternative? ...

How the transformation is possible ?

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven","eight", "nine" }; var textNums = from n in numbers select strings[n]; Console.WriteLine("Number strings:"); foreach (var s in textNums) { ...

Linq Compound selection

How can i combine two arrays in to a single array during compound selection ( without using Union ) ( The question was asked at interview). var num1 = new int[] { 12, 3, 4, 5 }; var num2 = new int[] { 1, 33, 6, 10 }; I tried as var pairs = from a in num1 from b in num2 select new {combined={a,b}}; Expected: combined ne...

Selecting combination using linq

This is also one of the interview question i have faced recently. Description : The task is $ 100(Please consider some currency) will be given to me.I need to purchase three items itemA,itemB,itemC.The cost of (I am not sure 0.25$ or 0.75 $ are meaningful,so think it as other currency) itemA=0.25$,itemB=0.75$ and itemC=20$. I need to p...

Handling temporary calculation in Linq

When solving an interview question Question A six digit number need to be found in such a manner when it is multiplied by an integer between 2 and 9 gives the original six digit number when its digits are reversed. Example: Suppose I multiply 219978 * 4 i get 879912 ,when reverse 879912 I will get 219978 back. I solved it us...

Solving brain teaser using linq

At the very outset let me express my sincere thanks to Marc Gravel,Dahlbyk and the rest for helping me to apply linq practically. The following are few questions which I have faced in an interview to solve applying Linq. As I am not familiar with Linq I solved it without using Linq. I appreciate the answers which helps me to solve t...

Linq Arithemetic Operator combinations

When attemptiing to solve the below assignment : Using the Arithmetic operators ( +,-,*,/) rearrange four fives to equal the number 1 to 10. Example : 5/5+5-5 =1 ,5/5+5/5=2 I tried in C# without using Linq (I don't know how to proceed further) public void GetDetails() { char[] sym = new char[] { '+', '-', '/', '*' }; ...

LINQ to objects vs for each - difference in execution timings

Hi, I am having this For each loop foreach (Account acct in acctTest) { if (acct.AccountId == acctId) { foreach (Customer cust in acct.CustomerColl) { if (cust.CustomerId == custId) { ...

Remove duplicates in the list using linq

I have a class Items with properties (Id, Name, Code, Price). The List of Items is populated with duplicated items. For ex.: 1 Item1 IT00001 $100 2 Item2 IT00002 $200 3 Item3 IT00003 $150 1 Item1 IT00001 $100 3 Item3 IT00003 $150...

Is there an application for displaying some kind of query plan for a Linq to object query?

I'm looking for an application to display what a linq expression would do, in particular regarding the usage of multiple access to the same list in a query. Better yet, the tool would tell if the linq query is good. ...

LINQ Implementation query ?

I am setting two values based on two different conditions inside for each. Is it possible to implement the below in LINQ ? foreach(Customer cust in Customers) { If(Condition 1) condtion1Var =true; If(Condition2 ) condition2Var =true; } ...

Is there a way to analyse how a particular Linq-to-objects query will execute?

In the past, I've written Linq to SQL queries that haven't performed well. Using SQL Profiler (or similar) I can look at how my query is translated to SQL by intercepting it at the database. Is there a way to do this with Linq queries that operate solely on objects? As an example, consider the following Linq query on a list of edges i...

Linq continuous date takewhile

Main problem: how do i group elements by their Date, only if continuous and only if they match some properties? Details: Given this type of object: public class MyObj { public DateTime Date { get; set; } public string ConditionalValue1 { get; set; } public int ConditionalValue2 { get; set; } public int OtherValue { g...

How can I use Linq to to determine if this string EndsWith a value (from a collection)?

Hi folks, i'm trying to find out if a string value EndsWith another string. This 'other string' are the values from a collection. I'm trying to do this as an extension method, for strings. eg. var collection = string[] { "ny", "er", "ty" }; "Johnny".EndsWith(collection); // returns true. "Fred".EndsWith(collection); // returns false. ...

Using LINQ to Objects to find items in one collection that do not match another

I want to find all items in one collection that do not match another collection. The collections are not of the same type, though; I want to write a lambda expression to specify equality. A LINQPad example of what I'm trying to do: void Main() { var employees = new[] { new Employee { Id = 20, Name = "Bob" }, new...

Need help creating Linq.Expression to Enumerable.GroupBy

I am attempting to generate an Expression tree that ultimately calls a series of GroupBy methods on the Enumerable type. In simplified form I am attempting something like this: IEnumerable<Data> list = new List<Data>{new Data{Name = "A", Age=10}, new Data{Name = "A", Age=12}, new Data{Name = "B", Age=20}, new Data{Name="C", Ag...

Dynamic LINQ Queries.

Is it possible to create Linq Queries at runtime. Using an xml rule which can be translated to a Linq Query. ...