linq-to-sql

How would I design a repository to handle multiple data access strategies?

What would a skeleton design look like for a repository able to support multiple database layers using ASP.NET MVC and C#? I want to see what a design would look like if I support both LINQ to SQL and NHibernate. How would I create my database object, and call a method on it in my BLL layer? ...

Why isn't this short circuit in lambda working?

Why linq is trying to check second expression anyway? .Where(t => String.IsNullOrEmpty(someNullString) || t.SomeProperty >= Convert.ToDecimal(someNullstring)) What is usual workaround? Update: It is about LINQ to SQL, of course. It cannot translate to SQL. ...

Hierarchical Databinding with Linq to SQL

I am attempting what I think is a rather simple databinding scenario with Linq To SQL. I have a table of FacultyMembers, whose schema looks something like this: FacultyMemberID - int PK Name - nvarchar UniversityID - int FK to University table and so forth. There are various other string properties. I generate LTS DataClasses. I dr...

Translate this SQL Query in LINQ to SQL

How can I write a query similar to this one in LINQ to SQL SELECT Id, (Distance - someValue) as Res FROM Table WHERE Res < someOtherValue ORDER BY Res ...

Linq To Sql: Cannot implicitly convert type IEnumerable to IQueryable

Obfuscated Scenario: A person has zero, one or many pets. Using Linq to Sql, the need is to get an IQueryable list of pets for the given personID. Here's the poorly mangled/butchered/obfuscated portion of the ERD: Code: public IQueryable<Pet> GetPersonPets(int personID) { var personPets= from p in Person where ...

Difference between Linq to Sql, Linq, Typed Datasets, ADO.NET

I been wondering about this for a while. It seems like there are so many ways now I don't know when to use what? Or if there is even a point to learn them. Like I don't know if they basically do all the same things and just basically stick with one till you master it then maybe look at other ones. So when I was taking an ASP.NET course ...

Linq to SQL Repository question

Hi Experts, I am working on a similar project like NerdDinner (www.nerddinner.com). I have similar tables, but I have used a generic repository (copied from here http://www.codeproject.com/KB/architecture/linqrepository.aspx). It seems working without any problem. Here are the codes. IRepository.cs using System; using System.Collect...

measure linq to sql performance and stats

I have a web app that creates a DataContext at the beginning of the request and lets go at the end. I would like to have some handy stats for each page like - number of inserts and time spent - number of deletes and time spent - number of updates and time spent - number of selects and time spent I have it all set for inserts/updates/de...

Working around LinqToSQls "queries with local collections are not supported" exception

So, I'm trying to return a collection of People whose ID is contained within a locally created collection of ids ( IQueryable) When I specify "locally created collection", I mean that the Ids collection hasnt come from a LinqToSql query and has been programatically created (based upon user input). My query looks like this: var qry =...

LINQ: Specified Cast is not valid

I have RANDOM error (1 from 100 page loads) in following line of code: topic = TopicsContext.GetCurrentDataContext().tTopics.Where(t => t.ContentId == contentId).SingleOrDefault(); Both ContentId property and conntentId local variables are long. Most important - error occurs randomly, in most cases it works fine. Thanks in advance ...

How to programmatically hide a linq-to-sql column on a datagridview?

I'm binding a linq Query to a datagridview for the purposes of allowing a user to insert/update/delete. However I'd like to hide the Id column. but I'd rather not hard code the datagridview.columns[id].visible=false In the event the id column name changes. I've read that you can walk through reflection to get a run-time list of colum...

C# Linq to SQL Stored Procedures

I have a stored procedure that returns a scalar int. I am using C# and Linq to get the resulting data, but I get the following error when I compile: foreach statement cannot operate on variables of type 'int' because 'int' does not contain a public definition for 'GetEnumerator' My question is how do I get the resulting data if the st...

linq subquery returning null

I have an odd linq subquery issue. Given the following data structure: Parents Children ------- -------- Id Id ParentId Location HasFoo (obviously this is not the real structure, but it's close enough for this example) I'm able to run this ...

Using LinqDataSource with SELECT statement

I am using LingDataSource, and I know I can’t use join query. How/Where can I put the below SELECT STATEMENT inside the gridivew to display the DBO.TOTALHOURSLU.DISPLAY instead of the DBO.LEAVEREQUEST.TOTALHOURSEFFECT? SELECT dbo.LeaveRequest.TotalHoursEffect, dbo.TotalHourslu.Minutes, dbo.TotalHourslu.Display FROM dbo.Leave...

A query body must end with a select clause or a group clause

var result = from lr in _db.LeaveRequest join th in _db.TotalHourslu on lr.TotalHoursEffect equals th.Minutesselect new {lr.TotalHoursEffect, th.Minutes, tr.Display}; ERROR: in new ...

How to insert rows in tables having many-to-many relation in linqtosql

I am new to linqtosql. I have a database schema where, An employee can belong to more than one team and team can belong to more than one employee. So actually I have a, Employee table: EmpID(PK),EmpName, etc.. EmployeesTeam table: EmpID(FK),TeamID(FK) (These two make composite PK) Team table: TeamID(PK),TeamName,etc I added rows t...

Get value from relationed database

Hi SO, I have a question: I have two MSSQL tables, items and states, that are linked together via a stateid: STATES ITEMS ------------- --------------------------- | id | name | | id | name | ... | stateid V ^ |_____________________________________| So Items.StateID i...

Left join in Linq?

There are a lot of questions on SO already about Left joins in Linq, and the ones I've looked at all use the join keyword to achieve the desired end. This does not make sense to me. Let's say I have the tables Customer and Invoice, linked by a foreign key CustomerID on Invoice. Now I want to run a report containing customer info, plus...

Construct object from LINQ using same fields of another query

Hello all That's example SQL create view v_join as select m.* , d.oneDetail from master m, detail d where m.key = d.key LINQ var view = from v in dc.v_join select new { Master = ???? /// that is an issue, how can I construct the objects using same fields but from another query, Detail = v.oneDetail }; foreach (var entry i...

How do you left join in Linq if there is more than one field in the join?

I asked a question earlier about why left joins in Linq can't use defined relationships; to date I haven't got a satisfactory response. Now, on a parallel track, I've accepted that I need to use the join keyword as if there were no relationship defined between my objects, and I'm trying to work out how to express my query in Linq. Trou...