linq-to-sql

Making linq avoid using in memory filtering where possible

Consider the these two LINQ2SQL data retrieval methods. The first creates a 'proper' SQL statement that filters the data, but requires passing the data context into the method. The second has a nicer syntax but loads the entire list of that accounts projects, then does in memory filtering. Is there any way to preserve the syntax of th...

Order By missing in linqtosql select random row

Hi, I have the following linq query : IQueryable<Message> messagesQuery = (from message in _context.Db.Messages where message.MessageListId == item.MessageListId && !_context.Db.ScheduleXMessages.Any(x => x.MessageId == mes...

LINQ-TO-sql expression for parent entity

I am trying to create Linq Expression that I can use to query child entity for parent properties(PATIENT entity in this case), using this method: private IQueryable<LAB_ORDER> SelectFilter(string fieldName, string value) { var param = Expression.Parameter(typeof(PATIENT), "item"); var field = Expression.PropertyOrField(param, fiel...

Linq subqueries

Hi, I am trying to modify some open source code to change how it filters search results. I want to query by the 'name' of a record instead of it's key. (I have records with duplicate names I want to pull back, but different keys.) This pulls back all the records public static IEnumerable<ticket> GetTickets(stDataContext db, bool? ...

IQueryable Parent-Child

I need to cast: IQueryable<PARENT> to IQueryable<Child>. IQueryable<PARENT> query = GetParents(); IQueryable<CHILD> result = query.Select(t => t.Children); This doesn't work, unable to convert EntitySet to IQueryable. Thanks ...

insert many rows with one static field and fields selected from another table

what i am trying to do is this: INSERT INTO Table1 (id1, id2) ( SELECT id, 1 as id2 FROM Table2 ) except i CANNOT put '1 as id2' inside that select statement. so, as an example, i sort of want this: INSERT INTO Table1 (id1, id2 = 1) ( SELECT id FROM Table2 ) i'm putting this in a stored proc and the select statement is gen...

Expression for children entitySet parent properties

I need to build an expression that will work on a parent entity properties so will do the following: IQueryable<Children> allChildren = from e in context.Children select e; IQueryable<Children> filter = allChildren.Where(x => x.Parent.Name == "Value"); I created an expression of type Expression.Lambda<Func<Parent, bool>> for that bu...

Can I update 1 object only with Linq to SQL?

Its a simple question, but I'm not aware of the answer and I couldn't get it to work. Can I update only one entity on the entire DataContext? Or should I follow plain ADO.NET for this operation only? Edit: public MyObject GetMyObjectById(int selectedId) { DataContext db = _dbManager.GetContext(); return db.MyObjec...

Linq search that ignores nulls

How can I make a linq search that ignores nulls (or nullables)? I have a method IEnumerable<X> Search(int? a, int? b, int? c) And I want it to return matches on any of the ints? that are not null. IE: if a and c have values 1 and 9 and b is null the search should render (roughly) to SELECT * FROM [TABLE] WHERE a = 1 AND c = 9 M...

Linq to SQL inheritance patterns

Caveat emptor, I'm new to Linq To SQL. I am knocking up a prototype to convert an existing application to use Linq To SQL for its model (it's an MVVM app). Since the app exists, I can not change its data model. The database includes information on events; these are either advertising events or prize events. As such, the data model inc...

Switch `ConnectionStrings` between local and remote with Linq to SQL

First, by Remote I mean a central dedicated SQL Server on our Network. By Local I mean a local SQL Express install. My situation is in house only. No cloud services, external sites, etc. My app allows the user to work disconnected but in order to minimize traffic and a few other issues as well I would like to allow them to connect st...

Typed DataSet vs Linq to SQL

I am new to LINQ.I used Strongly Typed DatSet. What is lacking in Typed DataSet to go for LINQ to SQL? ...

how can i query an already loaded data in EF ?

Hi I have an object Customer with a 1-n relationship to Addresses. I want to be able to take the first address. So i create a method: public Address firstAddress { get { var f=from d in this.Addresses select d; return f; } } I get the following error : Error 5 Impossible to find an i...

Help explain this linq query and how can it be improved?

Can this linq query be made more concise and improved? And can someone explain the original as well as an refactored version: Owners.GroupJoin (OwnerSubs, tl => tl.Unq, j => j.Unq, (tl, tl_j) => new { tl = tl, tl_j = tl_j ...

What are the fail scenarios for simply writing an image to the OutputStream of an EmptyResult?

I put together the following method, which calls my repository to get an image (returned as Linq.Binary). It then writes the byte array to the OutputStream and sets the content type: public EmptyResult GetImage(int id) { Byte[] imageBytes = _repository.GetImage(id).ToArray(); Response.OutputStream.Write( imageB...

Help with LINQ query

I'm trying to do this with LINQ: SQL>> SELECT p_Id from items WHERE p_Name = 'R1' LINQ>> var getpID = (from p in myDatabaseDataSet.Items where p.p_Name == ptxtBox.Text select p.p_Id).SingleOrDefault(); int p_Id = getpID; But getpID always return 0. What's wrong?. E...

LINQ Show every Record from table A exactly once when it exists at least once in Table B.

I have to tables, Table A is master, table B child. What is the simplest LINQ2SQL Query to show all records from Table A exactly once that have at least a child in table B? ...

Linq2Sql -How to write group by query

Hi, If I have the following fields in a table called DailyLeaveLedger LeaveDate LeaveType AmountPaid How do I write a linq2sql group by query to get this result where it groups by year and Leavetype and gives a column of the count of leave days and the sum of amount paid?? Sorted in descending order of year. 2010 Annual 10 5...

Case insensitive string compare with linq-to-sql and linq-to-objects

see also Differences between LINQ to Objects and LINQ to SQL queries We are using the some queries over our database and our in memory objects. What is the best way of doing an insensitive string compare with linq-to-sql so that? It runs fast on SQL Server The same query expression can be used with linq-to-objects to get the same res...

ASP.NET Requests... what do to with a Linq DataContext?

Sorry, if this is a duplicate. Please point me to the appropriate question if this is but I could not find exactly what I am looking for. So I am using a Linq to SQL datacontext for entity tracking and persistence in an ASP.NET web application. It is for an Intranet application that does not have a ton of users at a time. Right now I ca...