linq-to-sql

Store MS Word .doc in database and then get it out again

To store the word doc in SQL I have this: byte[] bytes = new byte[uploader.UploadedFiles[0].InputStream.Length]; var storedFile = new email_attachment(); string strFullPath = uploader.UploadedFiles[0].FileName; string strFileName = Path.GetFileName(strFullPath); storedFile.email_attachment_id = G...

DRY LINQ Predicate Filters for Multiple Tables

Suppose I have two tables, TableA and TableB. Each record in A has one or more related records in B. Say I want a reusable filter using predicates. I might do something like this (Linq-to-SQL by the way): private Expression<Func<ARecord, bool>> FilterPredicate() { return x => x.Name == "Test"; } private IQueryable<ARecord> GetRecor...

Linq-to-sql - query is not filtered

Hi, I am really new to Linq and am using Linq-to-Sql as follows. However in the following example, my where clause never gets executed and the resultant query attempts to fetch all the records from my table, ignoring even the take method. Can somebody point out as to what i am doing wrong var baseQry = db.Table; baseQr...

Execute multiple aggregates at once (SQL, LINQ).. or with better performance?

I have a table with records which include a datetime column "CreationDate". I need to get the following information for every of the last 90 days: How many records were there in total in existence How many records were added on that day I could do this through a loop of counting of course, but this would hit the database 90 times.....

Equivalent LinQ Query

Can someone help convert this query to SQL. I need this in linQ and I dont have write perms to get alternative for Storedprocedure. So linQ is the only way for me now to get it used with Silverlight SELECT ISNULL(COUNT(BGID),0)AS BGCOUNT,CASE SOURCE.PRIORITY WHEN 1 THEN 'P1' WHEN 2 THEN 'P2' WHEN 3 THEN 'P3' END AS PRIORITY FROM (SEL...

Merge an Object that wen outside the datacontext

Hi, I have the following question: It is easy to insert an oBject in database with a form. Just create an object link it to the fields in your from. Post back to controller, create a new datacontext and do datacontext.InsertOnSubmit(object) . public static void AddPage(string lang, Page page) { ...

Linq-To-Sql, MVC, Enumerable results with Distinct query

I'm very new to .Net C# and I really don't have a clue as to what I'm doing wrong. I have a LinqToSql DataContext I'm drawing distinct results from, but I'm not sure how to properly output the results in my view. I'm getting a compilation error (Compiler Error Message: CS1002: ; expected) on my view. StoreRepository.cs public IQuerya...

how to compare a string column value in a LINQ query?

I need to retrieve a set of "TOP n" number of rows from a DataTable where the table rows are Ordered By "Column X", but only if value of "Column X" for a row is greater than a provided comparison value. This is what I have so far: EnumerableRowCollection query = from history in dt.AsEnumerable() where hi...

Cannot Attach or Add an entity that is not new??

I have read the various posts regarding this error message and have typically avoided this problem in the past, but still haven't been able to figure this one, why am I getting this error: System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContex...

How to persist a changes in a DBML (Stored Procedure)

i am using stored procedure and if i add/remove parameters (in the sproc) than when i come to see in dbml than i dont see any changes so what i am doing currently is remove the sproc and added back to dbml designer. is this a normal behaviour of linqtosql? in the future if i end-up modify the sproc for some reason do i need to end-up ...

Can someone explain what is wrong with this Linq-To-Sql code?

See below: Edit.aspx View: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Test.Models.Friend>" %> Edit <h2>Edit</h2> <% using (Html.BeginForm()) {%> <%= Html.ValidationSummary(true) %> <fieldset> <legend>Fields</legend> <div class="edit...

Caching a LINQ to SQL DataContext

We're in the process of doing some performance optimization for a multi-tenant Web application. Currently, a LinqToSql Data Context is created at the beginning of each web request. The context has a lifetime for the web request and it's injected into the constructor of any objects that need it using Castle Windsor. We had the thought ...

ForeignKeyReferenceAlreadyHasValueException when not trying to change the foreign key?

In the following code, I don't believe I am changing the foreign key, but I could be wrong. When I run it, I get the exception: ForeignKeyReferenceAlreadyHasValueException Here is the code: UsersModule um = dc.UsersModules.Where(x => x.UserId == IdParam).FirstOrDefault(); int oldModuleId = um.ModuleId; string oldModule = um.Module.Mo...

How does Microsoft translate flat recordsets from Linq-2-Sql (or EF for that matter) queries into multiple objects?

My goal is to call a stored procedure that returns one recordset with columns from multiple tables, and have that recordset populate all related objects. This works perfectly using Linq-2-Sql (or EF) with DeferredLoadingEnabled=false and the LoadWith() option. At some point MS is taking the result set and matching the fields to the r...

question about LINQ

hi all. I have Three Tables in SqlServer (equivalent to image in link below in SQL Server) http://www.mojoimage.com/free-image-hosting-view-06.php?id=2Untitled-2.gif and when i use LINQ to Sql i get three classes in dbml file: class Item { Guid ItemId; int ItemSize; } class Video { Guid RecordId; Guid ItemId; string Resolution; } cl...

Resful WCF Service and LINQ

Hey guys / gals, Came across an interesting issue with the WCF Restful Webservice I am writing. It seems to be caching the LINQ data objects somehow. I will try to explain... This is my first venture into webservices and am not a LINQ expert so if I am poor at explaining this please bare with me... The webservice is a WCF Restful S...

Why is Linq to SQL trying to change the primary key of an entity when a child entity is assigned to it?

I don't follow: var refreshedSched = newTicket.Schedules .FirstOrDefault(s => CompareSchedules(s, sched)); if (refreshedSched != null) { if (refreshedSched.ScheduleExtension == null) refreshedSched.ScheduleExtension = new ScheduleExtension(); refreshedSched.ScheduleExtension.RTUpdateCurrent =...

Linq-to-Sql vs CRUD object?

I have a linq-to-sql object from a table. I want to store a similar data in the profile collection. Technically, I don't need all of the data in the linq2sql table and do not really prefer the legacy naming construct. My first thought would be to create a collection of CRUD objects. But, if I choose this solution I will have double the ...

How do I not return {ModuleName = Admin} from the linq query?

I am trying to get the string value "Admin" from a linq query, but when I do: string oldModule = dc.Modules .Where(x => x.Id == um.ModuleId) .Select(s => new {s.ModuleName}) .FirstOrDefault().ToString(); It returns { ModuleName = Admin } in the oldM...

LINQ to SQL: Single or Multiple .dbml (Sprocs)

i have read about the single vs multiple dbml but it was geared towards using tables but my questions is very specific to using sproc for multiple dbmls i have around 10 lookup sprocs and other i have getList/insert/update/delete .... the list is growing like crazy and its getting hard to keep eye on it and i am easily spending lot of ti...