linq-to-sql

Possible to convert IQueryable<Derived> to IQueryable<Base>?

I know about covariance, and I know that in general it will not be possible in C# until v4.0. However I am wondering about a specific case. Is there some way of getting converting IQueryable<Derived> to IQueryable<Base> by somehow creating a wrapper class that does not actually perform a query, but can actually "pass through" a .Wher...

LINQtoSQL Not saving to database,but changes show in app.

Hey all, I have a linq app using C# express2008 and sqlserver express 2005 (mdf file connection) I followed the regular dml generation and vanilla datacontext. However i created a repository class to manage the Linq stuff. In using the functions, selecting data works fine, updating data works in the app.But when i check the data in the ...

Having trouble doing an Update with a Linq to Sql object

Hi folks, i've got a simple linq to sql object. I grab it from the database and change a field then save. No rows have been updated. :( When I check the full Sql code that is sent over the wire, I notice that it does an update to the row, not via the primary key but on all the fields via the where clause. Is this normal? I would have ...

Union on two big LINQ queries not supported. How do I get around it (if at all possible)

This is quite a monster query I am writing. A very quick description of the database: You have an image table, an imagetag table, which joins it to a tag table. Images can have 0 or many tags. I have a query which does a full text search on the Image's title property and this works fine. However, I want it so when you do a full tex...

LINQ Outer Joins Dynamic OrderBy

In a Linq Statement like the following (narrowed down to relevant parts for the question): var sites = from s in DataContext.Sites join add in DataContext.Address on s.PrimaryAddress equals add into sa from a in sa.DefaultIfEmpty() select new { s.Id, ...

Help me to understand MY`Using` and `DataContext`

Can someone please explain the below to me. First is how I call the method and the second bit is the LINQ Method. My curiousity stems from the fact that I get a context error if I un-comment the using portion. Why? I apparently do not fully understand using and context's. And I would like to better understand this. Guid workerID =...

Select count for multiple dates

I have 3 tables: People, Group, and a PeopleGroup join table. PeopleGroup has a start and end date. Using linq-to-sql, I'm trying to write a query that will pull the count of group members on a 1-week interval for any given group over a specified date range. The end result would look something like (for group x in January): Date ...

Trouble understanding LINQ return value

I am trying to return the Actual Value in this query but I only get the Expression. Can someone point me in the right direction please. public static String NurseName(Guid? userID) { var nurseName = from demographic in context.tblDemographics where demographic.UserID == userID ...

asp.net mvc Relational data not being passed to view

Hi everyone, I currently have a repository setup that loads forms a person would need to fill out when a view controller action is accessed... the way i have it setup is as follows: public ActionResult View(long id) { Patient patient = patientRepo.GetPatient(id); if (patient == null) return View("NotFoun...

Linq2Sql generated classes able to put into a separate project?

Can anyone tell me if its possible to extract the linq2sql generated classes into a separate project in c#? - I presume I can just create the files and then copy them to a new project and add a reference to my Data project? The problem I have is that I have my UI, Service Layer and Data layer... Currently the data layer also has the ge...

Is there an efficient way to turn LINQ-to-SQL data into XML?

Hey Everyone, I'm looking at building a templating engine for my website. I'm using ASP.NET MVC and Linq-to-SQL. So, What I'm wanting to do is generate an XML document like this: <?xml version="1.0" encoding="utf-8" ?> <MainPage> <MainPageHtml><![CDATA[<p>lol!</p><br/>wtf? totally!]]></MainPageHtml> <Roles> <Role RoleId="1">...

See data in gridview in ASP.net with Linq

hi I want to show data in my database with Linq in gridview but I can't do it. this Code : var o = (from i in MDB.Messages select new { Subject = i.Subject, Message_Code = i.ID_Message, Question_date = i.Date, Question_Name = i.aspnet_Membership.aspnet_User.UserName }); EndInboxGrv.DataSource = o; How do I solve t...

How can I add this stuff up that is coming from a database?

Hi Say I have this in my database courseName : English101 Mark : 80 OutOff : 100 Weight : 50 CourseName : English101 Mark : 30 OutOff : 50 Weight : 10 CourseName: Chem101 Mark : 100 OutOff: 100 Weight : 100 Now I want to find the over all grade for both of these courses First english101. (80 /100) * 50 = 40(achieved weight) (30 ...

Best way to use XmlSerializer with XML field and LinqToSQL?

Is it possible to make LinqToSql use XmlSerializer to deserialize a typed XML field to an C# object? Currently I have the following code: (note that I had to use XElement.ToString() to use XmlSerializer) protected static T Deserialize<T>(string xmlString) where T : class { var xmlSerializer = new XmlSerializer(typeof(T)...

LINQ - Distinct() returning a different value if extension method used

I have a LINQ query which is attempting to get all of the distinct months of all of the dates in a table. I had this working using the Distinct() extension method. I then made it more readable by using an extension method to extract the month. And then it stopped returning Distinct results. Can anyone help me work out what happened her...

Updating multiple tables with LinqToSql in one unit of work

Table 1: int ID-a(pk) Table 2: int ID-a(pk), int ID-b(pk) Table 3: int ID-b(pk), string C I have the data to insert into Table 1. But I do not have the ID-a, which is autogenerated. I have many string C to insert in Table 3. I am trying to insert row into Table 1, get the ID-a to insert in Table 2 along with the ID-b that is auto-Ge...

Extract SQL Column Extended Properties From LINQ in C#

Hi, i have an SQL table with extended properties on each column. Is there a way to access these from LINQ in c# using Linq2SQL? ...

Linq to SQL - Retain DataContext across post-backs?

I read Rick Strahl's post about DataContext lifetime management, and some of the other related questions on Stackoverflow. If they contained an answer to my question, I must have missed it. I generally follow the atomic approach and instantiate a DataContext for a unit of work when it is needed, and dispose it afterwards. This worked w...

"An attempt has been made to Attach or Add an entity that is not new..." after calling ObjectChangeConflict.Resolve()

I have a pair of classes which, when updated together as shown below are guaranteed to cause a ChangeConflictException because a trigger on child object table updates a value on the parent object record. I believe I am following the correct procedure for resolving the conflict and resubmitting the update, but upon calling the second db....

Rollback with linq to sql?

I have this Public void CreateUser(params here) { CreateMembership(params here); Users newUser = new Users(); newUser.UserId = 123456 Context.Users.insertOnSubmit(); Context.Submit(); } public void CreateMembership(...) { Membership membership = new Membership(); membership.Something = "something"; Context....