linq

Similar features of LINQ

Is there any feature similar to LINQ (.NET) in different languages like JAVA, PHP, etc? ...

How can I compare two sets of data in linq and return the common data?

I have two IList<string> a and b. I want to find out what strings are in both a and b using LINQ. ...

Performance on joins in linq

HI , I am going to rewrite a store procedure in LINQ. What this sp is doing is joining 12 tables and get the data and insert it into another table. it has 7 left outer joins and 4 inner joins.And returns one row of data. Now question. 1)What is the best way to achieve this joins in linq. 2) do you think this affect performance (its o...

Enumerable Contains Enumerable

For a method I have the following parameter IEnumerable<string> tags and with to query a list of objects, let's call them Post, that contains a property IEnumerable<string> Tags { get; set; }. My question is: How do I use linq to query for objects that contains all the tags from the tags parameter? private List<Post> posts = new List<P...

Linq/C#: Selecting and summing items from the result of a group by?

Hi, I have a list like this: City Total Sydney 11 Dublin 9 London 12 Dublin 3 London 9 Sydney 12 I first of all need to Group By City & Sum Total so I have Sydney 23 Dublin 12 London 21 Next I need to filter for those that entries where the total is > 20 Sydney 23 London 21 And what I finally need is the total of these entrie...

LINQ multiple condition on "on" clause

I have a query which have multiple conditions on on clause SELECT * FROM CATALOGITEM with (nolock) INNER JOIN CATALOG with (nolock) ON CATALOGITEM.catalog_id = CATALOG.catalog_id and not(catalog.catalog_id = 21) AND NOT(catalog.catalog_id = 20) INNER JOIN PRODUCT with (nolock) ON CATALOGITEM.s_num = PRODUCT .s_num LEFT OUTER JOI...

Design advice. Using DataTable or List<MyObject> for a generic rule checker

Hi, I have about 100,000 lines of generic data. Columns/Properties of this data are user definable and are of the usual data types (string, int, double, date). There will be about 50 columns/properties. I have 2 needs: To be able to calculate new columns/properties using an expression e.g. Column3 = Column1 * Column2. Ultimately I woul...

how do you add a condition to a lambda expression

if i have this code today to find out a sum total using LINQ: return (MyArray.Sum(r => r.Trips); and i want to only include itms where r.CanDrive == true. can you add a condition into a single linke lambda expression? how would you do this ...

Linq to sum on groups based on two columns

Hi, I have a class as below: Class Financial { string Debit; string Credit; decimal Amount; } And I have a list with objects of this class, with multiple records. All I need is to perform a groupped sum, something like in sql Select debit, Credit, sum(amount) group by Debit, Credit I tried with a statement as below: f...

Interview Question: .Any() vs if (.Length > 0) for testing if a collection has elements

In a recent interview I was asked what the difference between .Any() and .Length > 0 was and why I would use either when testing to see if a collection had elements. This threw me a little as it seems a little obvious but feel I may be missing something. I suggested that you use .Length when you simply need to know that a collection ha...

Linq to XML query

Hi there, I'm loading from a xml some information about city's points of interest, and my xml structure looks like this: <InterestPoint> <id>2</id> <name>Residencia PAC</name> <images> <image>C:\Pictures\Alien Places in The World\20090625-alien11.jpg</image> <image>C:\Alien Places...

Cannot use ternary operator in LINQ query

I can't figure out why I get a Object reference not set to an instance of an object. error if I use a ternary operator in my LINQ query. var courses = from d in somesource orderby d.SourceName, d.SourceType select new { ID = d.Int...

VB.NET and linQ: How to delete entries from a dictionary using the value

I have a dictionary collection as bleow: mydic.addvalue(key1, val1) mydic.addvalue(key2, val1) mydic.addvalue(key3, val1) mydic.addvalue(key4, val2) mydic.addvalue(key5, val2) From the above dictionary I want to delete all the entries where value == "val1", so that the result would have only following entry: mydic.addvalue(key4, val2...

Setting a property from one collection to another collection

I have two colluections List<Application> myApps; List<Application> yourApps; These lists have overlapping overlapping data but they are coming from different sources and each source has some missing field data. Application object has a property called Description Both collections have a unique field called Key i want to see if t...

.NET - Very strange NullReferenceException?

How can I get a NullReferenceException in the following scenario? Dim langs As IEnumerable(Of SomeCustomObject) = //some LINQ query If langs Is Nothing Then Return Nothing If langs.Count = 1 Then //NullReferenceException here What am I missing here? Debug shows that langs is really just a LINQ queryresult without any results... ...

How to turn this LINQ query to lazy loading

I would like to make a certain select item to lazy load latter in my linq query. Here is my query var posts = from p in context.post where p.post_isdeleted == false && p.post_parentid == null select new { p.post_date, p.post_id, p.post_titleslug, ...

LINQ: how to transform list by performing calculations on every element

Hi, I have a class Class MyObject { decimal v1; decimal dv1; decimal v2; decimal dv2; } and a List<MyObject> ... I need to process every element of the list by adding dv1 to v1 and dv2 to v2 Something like (pseudo-syntax): myList.Transform(o=>o.v1+=o.dv1, o.v2+=o.dv2) How can I do this (obvious my pseudo-syntax...

removing items from a generic List<t>

I have the following method, I wish to remove items from my collection that match the product Id. Seems fairly straight forward, but i get an exception. Basically my collection is getting out of sync. So what is the best way to remove an item from a collection. public void RemoveOrderItem(Model.Order currentOrder, int productId) { ...

How extension methods work in background ?

I am just cuirous about behind of extension method mechanism.Some questions and answer appear in my mind. MyClass.OrderBy(x=>x.a).OrderBy(x=>x.b); I was guessing that mechanism was first orderby method works and order them by a member then returns sorted items in IEnumarable interface then next Orderby method of IEnumarable Order them...

C#/LINQ: How to define generically a keySelector for a templated class before calling OrderBy

Hi Folks, I have the following class defined in C# class myClass<T,U> { public T PropertyOne { get; set; } public U PropertyTwo { get; set; } } I need to write a function that reorder a list of myClass objects and takes two other parameters which define how I do this reorder: does my reordering depend on PropertyOne or Proper...