linq

Can you create a simple 'EqualityComparer<T>' using a lamba expression

IMPORTANT : THIS IS NOT A LINQ-TO-SQL QUESTION. This is LINQ to objects. Short question: Is there a simple way in LINQ to objects to get a distinct list of objects from a list based on a key property on the objects. Long question: I am trying to do a Distinct() operation on a list of objects that have a key as one of their propertie...

How to work with dataset's datarelation in linq?

Can someone provide an example of linq querying various nested tables in a dataset? I can't find something like that on the net. Thanks ...

Order two different C# lists with LINQ

I have two C# (3.5) types that are different from each other. I do not control them so I can not make them inherit from a common type or implement an interface. But they are very similar in "shape": class A { string Name string Data } class B { string Title string OtherStuff } class C { string ID string ExtendedData } List< A > myALi...

Delete function skipped in asp.net

Hello everyone I am trying to make a page which allows me to delete job applicants from the db. My page works fine but whn i click on the delete button it reloads the page but doesnt delete the applicant and his details. I am using linq. Here is my code <%@ Page Language="C#" AutoEventWireup="true" CodeFile="ApplicantManagement.as...

LINQ to DataSets for MySQL Interop

I'm building an application that must support MSSQL and MySQL databases. To avoid duplication of stored procedures etc., I'm considering using the Data Access Application Block to retrieve broadly scoped, very general datasets from either database, and then use DB agnostic LINQ code for more specific data access. My other option is to ...

Duplicate rows when using orderby, Skip() and Take() with LINQ

I'm using the following code to query my database: private const int PAGE_SIZE = 10; public static IList<Image> GetTopImagesForUser(String connectionString, int userID, int page) { dbDataContext db = new dbDataContext(connectionString); var images = (from p in db.Images where (p.SubmitterUserIndex == userID &&...

Linq - How to aggregate the results of another query

Hi, I want to take the results of a where clause on a list and then take that result set and create just one new type that has all its fields constructed from aggregates of the orginal query. So given the basic example below, is there anyway to combine the 2 linq statements into one? If the orginal where has no rows then it should re...

Flattening Linq Group query

I have a list of data like so: ID AddressPurpose Address ... 1 L 1 P 2 L 2 P 3 P 3 L 4 P 4 L 5 P 6 L I want to be able to filter the data so that for each unique number if there is a P row then it is returned else the L row is returned. So the data will look like this: ID AddressPurpose Address ... 1 P 2 P 3 P 4 P 5 P ...

How to use Stored Procedures w/ ASP.NET DataGrid

Question can also be: What is your preferred way of invoking stored procedures to fill a DataGrid? I am currently developing an ASP.NET Page, and I would like to know if Linq is the right way to go for use with my SQL Server DB. ADO seems nice too, so I would just like to have feedback on what is the most appropriate in general terms...

Using value from related table in LINQ where clause

I was expecting the following LINQ query to retrieve all contacts with the specified phone number but instead it returns all contacts that don't have a phone number at all. var query = from contact in dc.Contacts where contact.Phones.All(phone => phone.PhoneNumber == "5558675309") select contact; What am I doin...

how to access new manuplated combined columns in MVC asp.net using linq

Hi, I am facing problem in displaying the column field value at the view. I am returning view like follows. var TicketList = (from T in db.Tickets where T.Title.Contains("a") select new { customerName = ( from cname in db.Customers ...

LINQ and .COUNT timing out

I have a general question, and curiosity about LINQ and timeouts. I have a live application running and I am receiving timeouts on the following code. The following code, which is normal, and I don't see anything wrong: private static tblUser GetUserLinq(string email, string password) { DataContext db = new DataCon...

Merge two object lists with linq

I have the following situation Class Person { string Name int Value int Change } List<Person> list1 List<Person> list2 I need to combine the 2 lists into a new List in case it's the same person the combine record would have that name, value of the person in list2, change would be the value of list2 - the value of list1. C...

Parsing XML and cast elements to a typed collection using LINQ, C#

Hi I have an XML doc: <statuses> <status> </status> <status> </status> </statuses> I have parsed this into an XDocument, and want to use LINQ to select the elements into a strongly typed collection of Status classes (all status elements are simple types, either string or int). Any ideas how I can do this? Thanks! ...

linq question: querying nested collections

I have a Question class that has public List property that can contain several Answers. I have a question repository which is responsible for reading the questions and its answers from an xml file. So I have a collection of Questions (List) with each Question object having a collection of Answers and I'd like to query this collection o...

[LINQ] Operations on Lambda Grouping

Hello, Im stuck with a LINQ group by situation trying to solve it without using foreach statement, here is the escenary: I Have two generic collections List<OrderHeader> and List<OrderDetail>, both have a same field "TOTRGVS" that contains total amount from a order, and the number of order is the key named "NDORGV". Then I want to foun...

Sorting a list using Lambda/Linq to objects

I have the name of the "sort by property" in a string. I will need to use Lambda/Linq to sort the list of objects. Ex: public class Employee { public string FirstName {set; get;} public string LastName {set; get;} public DateTime DOB {set; get;} } public void Sort(ref List<Employee> list, string sortBy, string sortDirection) { ...

Sudoku algorithm in C#

I need one liner (or close to it) that verifies that given array of 9 elements doesn't contain repeating numbers 1,2,3,...,9. Repeating zeroes do not count (they represent empty cells). The best I have came out so far is: var a = new int[9] {1,2,3,4,5,6,7,8,9}; var itIsOk = a.Join(a, i => i, j => j, (x, y) => x) .GroupBy(y => y).Wh...

Any chance to get unique records using Linq (C#)?

Hi, I got a list<list<string>> in list[x][0] are records from which I want to choose unique records thus such record wouldn't be in any other list[x][0], when I choose it, i'd like whole row list[x] to be chosen. I haven't found the appropriate exapmple for this in Linq, please help :( EDIT When Jon Skeet asks me to clarify, I can't ...

LINQ is able to filter on association, but returns 0 results when referenced directly

I am a little confused with a statement I am testing out. I am working with LINQ to Entities I have commented in the code below. It is the last Console.WriteLine which I do not understand. I appreciate anyones input on this using(Ecommerce.Data.Entities.EcommerceEntities ents = new Ecommerce.Data.Entities.EcommerceEntities()...