linq

LINQ multiple childs in 1 row

My tables are structured as below: Questions id title 1 what is this? Answers id answer qid 1 an 1 I want to select the question and all the answers in one single row through LINQ? How can i do this? ...

Constructing a Sorted List with priority items

I have a list of news unsorted items, some of which have a priority flag. I need the priority items to float to the top of the list, and then sort the rest by a date. So, the end result is a list of news items that has the priority items showing at the top and the remainder sorted by date. There has to be a better way to do it than th...

Linq query Join objects with different data sources?

I am trying to join two objects, the first is a (static) local object defined in a Helper and the second (Subsonic) Database object. Here is the offending extract from my repository, I wont bore you with the models and helpers unless requested. public IQueryable GetData(string DataType) { IQueryable<DatabaseObject> dat...

Simple sql to Linq query with group by and aggregate functions

I'm fighting with linq trying to learn the syntax and I can't figure out how to do the following simple query SELECT DISTINCT user.firstname, user.lastname, COUNT(invoice.amount), SUM(invoice.amount) FROM company_user INNER JOIN user ON company_user.user_id = user.user_id INNER JOIN ...

DTO to DO - How to flatten DTO's into a DO collection property

I would like to flatten a collection of DTO's into a single DO using LINQ, but my LINQ-fu is weak. My DTO looks like this: public class DTO { Product product, Location location, MonthPeriod month, double Data } and maps to a SQL database table with this schema: ProductID int, LocationID int, MonthPeriodID int, Data numer...

what does this .net line of code means.

Hi all, I recently shifted from JAVA development environment to .net development environment. I am developing web application using .net MVC framework. Would someone help me to find the meaning of following code segment. It seams like iterating thought list, but I could not find specific definition of this code sample: SmartTextBoxMod...

NHibernate LINQ throws "The binary operator Equal is not defined for the types 'System.Collections.Generic.IList`1[System.Int32]' and 'System.Int32'"

The following LINQ to NHibernate (using NHibernate 3.0) results in a System.InvalidOperationException being thrown with the message "The binary operator Equal is not defined for the types 'System.Collections.Generic.IList`1[System.Int32]' and 'System.Int32'" public IEnumerable<ProjectSummary> GetProjects( IList<int> clients ) { usin...

Could table-returning select Oracle query from stored procedure be transformed into LINQ?

Could this Oracle query:select * from table(some_stored_procedure(someParameter => :someValue) be transformed into LINQ? ...

Entity Framework Best Practices In Business Logic?

I am using the Entity framework for the first time, and would like to know if I am using in the best practice. I have created a sperate class in my business logic which will handle the entity context. the problem I have, is in all the videos I have seen they usually wrap the context in a using statement to make sure its closed, but ob...

In LINQ, does orderby() execute the comparing function only once or execute it whenever needed?

I found a method to shuffle an array on the internet. Random rand = new Random(); shuffledArray = myArray.OrderBy(x => rand.Next()).ToArray(); However, I am a little concerned about the correctness of this method. If OrderBy executes x => rand.Next() many times for the same item, the results may conflict and result in weird things (po...

Group using linq return datatable

i have a linq query that returns Articles ordered by the number of tags that match the current article e.g current article has tags - tag1, tag2, tag3 tagged article 1 - tag1,tag2,tag3 tagged article 2 - tag1,tag2 linq i have is DataTable query = (from row in dt.AsEnumerable() let tags = row.Field<str...

Problem transforming dictionary with Linq

I've got a dictionary laid out like so: Dictionary<string, List<Series>> example = new Dictionary<string, List<Series>>(); example.Add("Meter1",new List<Series>(){ new Series{ name="Usage", data = new double[] {1,2,3}}, new Series{ name = "Demand", data= new double[]{4,5,6}}}); example.Add("Meter2", new Lis...

LINQ way of transforming "TheQuickBrownFox" into "The quick brown fox"

Hi all, I am trying to transform a string made of words starting with an uppercase letter. I want to separate each word with a space and keep only the first uppercase letter. All other letters should be lowercase. For example, "TheQuickBrownFox" would become "The quick brown fox". Obviously, I could use a simple foreach and build a st...

Linq-to-Entities: LEFT OUTER JOIN with WHERE clause and projection

I'm having a heckuva time figuring out how to translate a simple SQL LEFT OUTER JOIN with a two condition where clause into a working Linq-to-Entities query. There are only two tables. I need values for all rows from Table1, regardless of matches in Table2, but the WHERE clause uses fields from Table2. In SQL, the two parameters would...

FormViewModel in MVC for Relations

I am very new to MVC and LINQ.And have some questions. I have a table named users where I am storing "M" or "F" for male and female.There is another table called Genders Which is linked to the Users table.I have Users Model in ASP.NET MVC application.My question is when I show data where should I create a property to convert that "M" to...

How would a write a LINQ query to compare with a list of values ?

I have basically to select all employee from companies which are being passed so a variable - CompanyListIds - Contains list of all company ids.. var result=DataContext.Employee(e=>e.CompanyId==companyId).ToList(); The above is a query I have for selecting from one company, now how would I modify it to compare with all the companyi...

Adhoc stored procs on silverlight app

I have a simple requirement to be implemented on a silverlight app. I want to list down all the stored procs in a drop down and view its output on a grid. I am currently having a linq to sql class where I have drag dropped all the stored procs I want to view and generated the classes on a .dbml file. This works for me at the moment. Howe...

How can I condense the following Linq query into one IQueryable

I have an EntityFramework model that has User entity that has an EntityCollection of Organisations. For a particular User I am trying to write a Linq Query to return the names of the organisations that the user belongs where that query will hit the db only once. My problem is that I cannot see how to write this query without having to ...

List<Product>.Join(List<Order>, ...) is identical to List<Order>.Join(List<Product>, ...) ?

Up to now, when doing join operation in LINQ, I have no idea how to decide which list must come first and which list must come after. Assume I have two list, List<Product> and List<Order>. Edit: My confusion is to decide List<Product>.Join(List<Order>, ...) or List<Order>.Join(List<Product>, ...) ? ...

LINQ: Select from IEnumerable with Distinct/GroupBy and sorting - possible?

My particular example is fairly complex but I think the concept would apply equally to something like a logging system so I'll use that instead for ease of explanation. It's a ficticious example, please don't harp on or agonise over what is achitectually, programatically or morally wrong with the example itself :) Say you have this: cl...