linq

How to group by custom types with linq

I have this class public class Item { public Coordinate coordinate { get; set; } ... ... } With Coordinate being define like this: public class Coordinate { public Coordinate(float latitude, float longitude) { Latitude = latitude; Longitude = longitude; } ...

'orderby' in linq using a string array c#

Say I have a method definition as such: public CustomerOrderData[] GetCustomerOrderData(string[] CustomerIDs) { var query = (from a in db.Customer join b in db.Order on a.CustomerID equals v.CustomerID orderby CustomerIDs select new CustomerOrderData() { //populate...

Linq randomly returns periodic null results.

I've got a linq query, which is admittedly pretty large. I can't identify where it is going wrong because it ONLY happens on the remote server and I don't have the access to debug it. But it is basically like this... This code lists all of the 'folders' that the current member can see. (Folders contain images.) if (members...

Saving and retrieving inherited types with LINQtoSQL and Business Objects

I have an abstract EventBase class and some inherited event types, along with an Event class. Each event type has its own unique columns. In my data layer, I have a GetEvents method that simply does: from e in db.Events select new Event {...values...}; EventType is an enum which matches up to an EventTypes table I want GetEve...

Linq to Entities: Next Value in Sequence

im working on a LINQ to Entities project for school, i am working with a db2 database that generates key values through a generated sequence eg. INSERT INTO STUDENT VALUES(NEXT VALUE FOR STUDENT_NUM, 'Raphael', 'Joasia'); is the there an equivalent to the NEXT VALUE operator in LINQ to Entities that will do the same or will i need to c...

Linq Query Puzzle

If I have an object with a parent child relationship with Id and ParentId representing the unique Id of the record and the Parent Id of the record. I need a query that will query a list of objects and return the path to the root of the relationship for each item (i.e. Path = (AllTheParentNames)\ Name ). Any help with this would be appr...

How do I format date literals in dynamic linq?

I am using dynamic Linq to return data for user-input search criteria. My query is working fine except for the user selected dates. My current code is: StringBuilder whereClause = new StringBuilder(); if (startDate.HasValue || endDate.HasValue) { DateTime searchStartDate = startDate.HasValue ? star...

linq sum string as decimal

I have the following LINQ : Dim q = From p In ds_raport.Tables(1) _ Group p By p!Cod_Prj Into g = Group _ Select New With {g, .TotalVal = g.Sum(Function(p) p!Valoare)} The problem is because column "valoare" is of type string ( i import this from a file that is not always properly formatted) my sum has no decimals. So...

How to join two collections by index in LINQ

What could be a LINQ equivalent to the following code? string[] values = { "1", "hello", "true" }; Type[] types = { typeof(int), typeof(string), typeof(bool) }; object[] objects = new object[values.Length]; for (int i = 0; i < values.Length; i++) { objects[i] = Convert.ChangeType(values[i], types[i]); } ...

Check if multiple values (stored in a dedicated collection) are in a LINQ collection, in query

Hi, What is the method in LINQ to supply a collection of values and check if any/all of these values are in a collection? Thanks ...

linq sum in vb.net

I need to make a query that look like this is SQL: SELECT CodProiect, SUM(Valoare) FROM DET WHERE CodProiect = 'cod_pr' GROUP BY CodProiect; How can I write the same thing in LINQ? I have attempted this: dim gac1 = (From ac1 In t_detalii _ Where ac1!CodProiect = cod_pr1 _ Select ac1!Valoare).Sum(Function(ac1...

Read text data from file using LINQ

I have following text file: 37 44 60 67 15 94 45 02 44 How to read all numbers from this file and save them into two-dimensional array, using LINQ? All I manged to do was creating a simple array with all first values in each row. Is using LINQ in this case a good idea or should I simply load the file normal way and parse it? ...

Invalid Cast Exception while using LINQ

Hello =) I'm a classic newbie... i can compile my program, but get an invalidcast excpetion when running the programm what's wrong with my code. i don't not what to seach for :( This is my code (.net 3.5) class Element{ private int i; public int I { get { return i; } set { i = value; } } private string s; public st...

ASP.NET ReportViewer and Linq To Sql Questions

Does the asp.net ReportViewer control work with LinqDataSource? If so are there any examples out there? Why don't Linq to Sql generated entity classes show up in the Website Data Sources pane, when editing a report? ...

What type should I use with this aggregate query?

I am just getting my feet wet with LINQ to SQL, and need some advice in implementing a simple scenario: I have a method ListTagCounts that is doing an aggregate query with LINQ To SQL. I was not sure of the return type to use, so used the ToList() method and created a class just for the return type in order to get it to compile. Schema...

In LINQ-SQL, wrap the DataContext is an using statement - pros cons

Can someone pitch in their opinion about pros/cons between wrapping the DataContext in an using statement or not in LINQ-SQL in terms of factors as performance, memory usage, ease of coding, right thing to do etc. Update: In one particular application, I experienced that, without wrapping the DataContext in using block, the amount of me...

How do I use Linq to group a DataTable by multiple rows/columns into a new datatable with duplicate rows concatenated?

God, Linq confuses me... I've found quite a few examples that do similar things to what I need, but I'm still getting all turned around. I have a table (we'll call it UngroupedTable) that looks like this... ID, Val1, Val2 0001, A, 1 0001, B, 2 0002, C, 3 0003, D, 4 0003, D, 5 I want to use Linq to build a table where essentially in Gr...

Linq Expression Syntax - How to make it more readable?

I am in the process of writing something that will use Linq to combine results from my database, via Linq2Sql and an in-memory list of objects in order to find out which of my in-memory objects match something on the database. I've come up with the query in both expression and query syntax. Expression Syntax var query = order.Items.Jo...

List<T>.ForEach vs. custom IEnumerable<T> extension

Say I have a class: public class MyClass { ... } and a webservice method that returns an IEnumerable<MyClass> The consumer of the webservice defines some method: public void DoSomething(MyClass myClass) { ... } Now, the consumer can call DoSomething on the result of the webservice method in two ways: var result = // web ser...

What's a Linq query that will get each objX where objX.created = {newest}?

This one's turning out to be a brain teaser for me. I almost hate to ask for help for fear I might miss out on the endless, sleepless night trying to cypher this mystery. JK I've got a C# project where I need to display a list of unique objects, but only the newest one based on the type of object. For discussion purposes, let's talk "fr...