linq

how to convert time field into string using linq sql?

Hi, i am using linq to fetch the data and converting time field into string. At that time if i have a value 11:00:00 in time field of sql server. But i am getting "Jan 1 1900 11:00AM" at the time of converting to string. Please provide me help to fetch only time part. Thanks in advance.. ...

How to write a LINQ query to do this...

IQueryable<SomeType> result = (from x in Context.XXX select x); now what I need to do is the following (written pseudo code, I need actual code): foreach(var i in items) { // Where the object "i" has 2 properties // 1) i.Operator (values of which can be AND or OR) // 2) i.SomeID (values of which can be 1 .. 10) // I need to b...

In LinqToSql how do you enable change tracking on an entity's custom property?

For other reasons, i've had to create my own property in an Entity in the ORM, which has is a type of another entity (had issues with associations so did it this way instead). The problem is that whenever I make a change to that property, it isn't being flagged as a change so I cannot call SubmitChanges on it. So basically my question ...

Linq to SQL multiple Left joins using multiple fields and subquery

Basically I want to be able to recreate this query in Linq SELECT * FROM Customers LEFT JOIN Orders LastOrder ON LastOrder.CustomerId = Customers.CustomerId && LastOrder.CreatedOn = (Select MAX(CreatedOn) FROM Orders WHERE CustomerId = Customers.CustomerId) LEFT JOIN OrderStatus ON OrderStatus.OrderStatusId = LastOrder.OrderStatusId...

Can I call a Method in one DataContext from another?

I have the below LINQ Query that I would like to suppliment with the Nurse Names. Unfortunately these are stored in a seperate DB and thus a seperate DataContext. How would I go about calling a method in my aspnetdbcontext from this query, which uses cmodatacontext? This is the method in aspnetdb to get a nurse's name: internal s...

Is there any way to make Code Contracts work with LINQ?

Code Contracts keep giving me "Possibly calling a method on a null reference" warnings for all of my LINQ statements, and I can't find a way to silence them. For example, the following method generates two such warnings because I'm accessing the "Make" and "Model" properties of the "car" object without first checking for null. publ...

LINQ Filter anonymous type based on IEnumerable values within type

I'm using LINQ to SQL like: var b = from s in context.data select new { id = s.id, name = s.name myEnumerable = s.OneToMany }; Where myEnumerable is of type IEnumberable<T> and I want to now get a subset of b based upon properties of the individual items of myEnumerable. For example, say <T> ...

What's the LINQ'ish way to do this...

Say, I have an array of lists, and I want to get a count of all of the items in all of the lists. How would I calculate the count with LINQ? (just general curiosity here) Here's the old way to do it: List<item>[] Lists = // (init the array of lists) int count = 0; foreach(List<item> list in Lists) count+= list.Count; return count...

How can i use "like" in linq to xml

I wanna do something like this. I know it’s wrong: var a = from h in xdoc.Root.Elements() where h.Element().value like = "1234" select h; ...

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, ...

ADO.NET EF and LINQ pagging records & translation to SQL

Hi there I saw a ADO.NET EF and LINQ pagging structure like this: var query = ...your normal query here... int totalRecordCount = query.Count(); var pagedQuery = query.Skip(PageIndex*PageSize).Take(PageSize); This code seems to query all records setting them into the local memory, so it gets the total count. After that, it pages de r...

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 =...

Perform mutable updates in LINQ query?

I have a LINQ query where I want to return modified objects. If I were in an immutable Functional mood, I might do something copy-construtor-like, like this: from widget in widgets select new widget { legs = widget.legs + 1, arms = widget.arms } Sadly, I'm doing this on a mutable NHibernate entity object and I have to modify the origi...

Create Records with Linq to SQLite via DbLinq

I have (after some significant effort) gotten DbLinq working with the latest build of Mono on OS X. Has anybody successfulyl created database entities via DbLinq/Sqlite? For example, I have the following table: CREATE TABLE UserType ( id integer primary key, description text ) I have generated my *.cs file and am using the f...

How to compare only date components from DateTime in C#?

I am having two date values, one already stored in the database and the other selected by the user using DatePicker. The use case is to search for a particular date from the database. The value previously entered in the database always has time component of 12:00:00, where as the date entered from picker has different time component. ...

Linq: How to verify dbcontext.SubmitChanges() for updates/inserts succeeded?

How do you verify that SubmitChanges() worked for inserts and updates? The method doesn't return a bool or int value. ...

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 ...

Designing DATA Access Layer in ASP.NET 3.5 app

I need to decide about the DATA Access Layer of a medium sized web application to be run on the intranet of a company. I've worked with CodeSmith before and found it useful but it consumes lot of development time if the underlying database schema changes, so would like to do away with it and try my hands with some new DAL which would hel...

yield return versus return select

Which are the advantages/drawbacks of both approaches? return items.Select(item => DoSomething(item)); versus foreach(var item in items) { yield return DoSomething(item); } EDIT As they are MSIL roughly equivalent, which one you find more readable? ...

VB.NET equivalent to this C# linq class

Guys, I'm trying to convert something from C# to VB.NET and I'm having trouble finding an equivlent in VB.NET to C#'s yield keyword. I realize 'yield' is not a convertable keyword to VB.NET, so can someone please show me how I would implement this code in VB.NET. I got all of it converted over except for the implemented GetEnumerator() f...