linq

Converting Linq MemberExpression lambda to work on class with reference

For this question, I'll use the standard structure of Products (with an IsActive flag) and OrderItems (that each reference a Product). I also have a query builder that generates Linq expressions used to query products. A sample filter would let the user find active/inactive products, generating a Linq expression like: Expression<Func<...

OrderByDescending() per MSDN, what on earth does this mean?

Can someone please help be take apart the elements here and help me understand what they are? public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>( this IEnumerable<TSource> source, Func<TSource, TKey> keySelector ) What is TSource and TKey? What is a keySelector? What the heck is an IOrderedEnumerable? ...

Linq.NHibernate problem with OR statement

NOTE: all the code is written Top of my head. It can contains some mistakes. Just get the overall point of this question) Taking this class definition: (reduced for simplicity) public class CodedValue { public string Code { get; set; } public string Value {get; set; } } Taking thoses objects: CodedValue cv1 = new CodedValue(){ C...

Negating a method call in an Expression tree

I'm generating a c# Linq expression dynamically as below, which will (in the example below) run string.Contains against the collection values. var dynamicMethod = "Contains"; var parameter = Expression.Parameter(typeof (MyClass), "type"); var property = Expression.Property(parameter, "MyProperty"); var constantValue = Expression.Constan...

Handle a Dynamic Select With Dyanmic Linq

I am using the Dynamic Linq Library that Scott Guthrie describes here. Scott Guthrie's examples are great and I have used the dynamic Where statements quite a bit. Now, however, I am faced with a situation where I need to use the dynamic select functionality. Scott Guthrie shows a screenshot of this functionality (in the very last...

Data filtering or better LINQ query?

I am using the new WPF toolkit's Chart to plot large data sets. I also have a crosshair tracker that follows the mouse when it's over the chart area to tell exactly what is the value of the nearest data point (see Yahoo! Finance charts). I use the following code to find the closest data point that is lower (or equal) to where the mouse ...

Help with with a traslation in Linq to Xml.

Can someone help with an explanation of what this means: ... .Select(Func<XElement, XElement>selector) Please an example of what should go in as parameter will be appreciated. Also found it a little bit difficult naming this question. Suggestion will also be appreciated. ...

C# LINQ -How to apply group by?

I am new to linq.From the list of following data,help me how can i apply Group by and other construct to achieve the expected output as given below. List<SalesAnalysis> AnaList = new List<SalesAnalysis>(); AnaList.Add(new SalesAnalysis("P001", 2009, 45000)); AnaList.Add(new SalesAnalysis("P001", 2008, 13000)); ...

Convert a dictionary<string,string> to xml

I want to convert a dictionary<string,string> to this xml: <root> <key>value</key> <key2>value2</key2> </root> Can this be done using some fancy linq? ...

C# -Getting Possible pairs from two enums

From two enums ,what is the way to apply LINQ to get pairs like {Red,Car},{Red,Bike},{Green,Car},{Green,Bike},... public enum Color { Red,Green,Blue } public enum Vehicle { Car,Bike } can i use something like var query = from c in Enum.GetValues(typeof(Color)).AsQueryable() from c in Enum.GetValues(typeof(Vehi...

LINQ to SQL, sorting by a related table

I'm trying to order the values in a related table using LINQ to SQL. I have 2 tables. Menu and MenuSection. They are related one to many on Menu.MenuId == MenuSection.MenuId Currently, I'm pulling this information using the following query var menus = from m in _context.Menus select m; Thi...

How to most elegantly iterate through parallel collections in C#?

var a = new Collection<string> {"a", "b", "c"}; var b = new Collection<int> { 1, 2, 3 }; What is the most elegant way to iterate through both yielding a set of results "a1", "b2", "c3"? ...

Does Linq Exist for Javascript?

Just wanted to know if I can use Linq on Javascript arrays. If not is their a 3rd party tool I can use? ...

C# LINQ Autogenerated number or index

When deriving combination,what is the possible way to generate auto generate numbers. public enum Color { Red,Green,Blue } public enum Vehicle { Car,Bike } (i.e) {Red,Car},{Red,Bike},{Green,Car},{Green,Bike}...... (Jon Skeet helped me to solve it). var query = from Color c in Enum.GetValues(typeof(Color)) from V...

C# Linq queries

How to find Min,Max,Sum of given array? int[] number = { 1, 2, 3, 78, 100, 1001 }; (not working) var query = new { maximum = number.Max, minimum = number.Min, Sum = number.Sum }; ...

Nhibernate component with mapped class problem

I am working with oracle and nhibernate. I can select list of an object from db table (all items in table) as Iquerable, but when I try to select an item from the list using "linq where clause" it sends nonsense query to oracle db. And it gets invalid identifier error. I can get whole list without any error in query, it happens when I u...

C# Random Pair generation

Assume the possible outcome of a die thrown is one of {1,2,3,4,5,6} When two dice are thrown three times, I want to collect random outcomes from two dice. My implementation is var q = from threeTimes in new int[] { 1, 2, 3 } let set1 = new Random().Next(1, 6) let set2 = new Random().Next(1, 6) ...

inserting with linq

hi i am trying to insert a load of data into a table with linq the data arrives in a nameValueCollection with the key as the column name and the value as the value to be inserted i need to convert all the values to their correct datatype but cant think of a good way to do this and then insert i can iterate over the columns in the LINQ...

ASP.NET MVC 1 Linq to SQL Error

Hi. I have this call: List<Product> featProducts = productsRepository.Products.Where(x => x.Featured == true).ToList(); It returns the following error: System.InvalidCastException: Specified cast is not valid. I find it very weird as I am making similar calls in other places and it all works fine. My app compiles with no issue...

problem with delete when multiple columns

hiya when deleteing this works: orderitems.Delete(x => x.orderitem_sessionid == transkey); however this does not work orderitem.Delete(x => x.orderitem_sessionid == transkey && x.orderitem_productid == 6); i get no errors, but nothing is deleted either, i have working code as a substitute of var Deleteable...