linq

LINQ Query - Explanation needed of why these examples are different.

I'm reading the book "LINQ Pocket Reference" and there is a particular example (slightly modified below) that I'm having difficulty getting my head around... The explanation in the book is a bit brief, so I was wondering if someone could break it down step-by-step for me so that it makes sense... IEnumerable<char> query2 = "Not what...

When should I dispose of a data context

I'm currently writing a data access layer for an application. The access layer makes extensive use of linq classes to return data. Currently in order to reflect data back to the database I've added a private data context member and a public save method. The code looks something like this: private DataContext myDb; public static MyClass ...

How do I build a search mechanism for my application?

It seems to be a common requirement nowadays to have a search feature that can search almost anything you want. Can anyone give me samples or tips as to how to go about building a one stop search for an application? For example: you have 3 tables customers, products, employees. The application has a master page that has a textbox at the...

Make NameValueCollection accessible to LINQ Query

How to make NameValueCollection accessible to LINQ query operator such as where, join, groupby? I tried the below: private NameValueCollection RequestFields() { NameValueCollection nvc = new NameValueCollection() { {"emailOption: blah Blah", "true"}, ...

Adding related data with multiple LINQ to SQL DataContexts

Hi: I'm developing a desktop application using Linq to SQL. From what I've gathered, it's better to keep DataContext objects short-lived, but I'm running into the following puzzle: My application has lots of lookup-type data that I'd like to cache on the client. The reason I want to cache it is because I don't want to have to look up...

Best way to call an instance method in Expression Trees?

What is the best way to call an instance method within an Expression Tree? My current solution is something like this for an interface method "object GetRowValue(rowIndex)" of the interface IColumn. public static Expression CreateGetRowValueExpression( IColumn column, ParameterExpression rowIndex) { MethodIn...

Should parameters/returns of collections be IEnumerable<T> or T[]?

As I've been incorporating the Linq mindset, I have been more and more inclined to pass around collections via the IEnumerable<T> generic type which seems to form the basis of most Linq operations. However I wonder, with the late evaluation of the IEnumerable<T> generic type if that is a good idea. Does it make more sense to use the T[...

LINQ- Combine Multiple List<T> and order by a value (.Net 3.5)

Hi, all I'm trying to combine a number of List<T> where T:IGetTime (i.e T will always have method getTime()). Then I'm trying order the items by the DateTime that getTime() returns. My LINQ looks like this: public static List<T> Combine(List<T> one, List<T> two, List<T> three) { var result = from IGetTime item in one ...

Linq to SQL Class

Hi, I have a Class see below, when i am trying to query it from another page it not linking e.g. if i put "var Notes = HandhedNotes.GetNotesByLinkIDJoin();" when i look at Notes theres not links there. I think its due to the IQuerable maybe I should be using something elese?? namespace WebSys { public partial class HandheldNote { ...

Update all objects in a collection using Linq

Is there a way to do the following using Linq: foreach (var c in collection) { c.PropertyToSet = value; } To clarify, I want to iterate through each object in a collection and then update a property on each object. My use case is I have a bunch of comments on a blog post and I want to iterate through each comment on a blog post an...

Checking for the existence of an object in a collection (of T)

I see that over on this question http://stackoverflow.com/questions/312024/linqy-way-to-check-if-any-objects-in-a-collection-have-the-same-property-value there is a request to say how to do something using LINQ to see if a property matches in a collection. However, is this the fastest reasonable process by which to do this? I will be dep...

Linq returns list or single object

I have a Linq to Entities query like this one: var results = from r in entities.MachineRevision where r.Machine.IdMachine == pIdMachine && r.Category == (int)pCategory select r; Usually, I use the code below to check if some results are returned: if (results.Count() > 0) { return new o...

Linq and DeleteAllOnSubmit pain

The following code loads a gig, clears out the gigs acts collection and then adds a new act. Data.LinqToSQL.Gig dbGig = DBContext.Gigs.Where(x => x.ID == myGigID).SingleOrDefault(); //Remove all references to the current acts if(dbGig.Acts!=null) { DBContext.Acts.DeleteAllOnSubmit(dbG...

Do I have to close the SQL Connection manually if I use Linq?

Traditionally, when we use SQL string to done some work, we have to close the sql connection before the page being closed, I was wondering if I use Linq to do the data operations do I still need to close the connection manually? ...

Filtering Null values in Select

I have IQueryable list of objects of type T which I want to transform into objects of type K List<K> tranformedList = originalList.Select(x => transform(x)).ToList(); the transform function returns null if it cannot tranform the objects.If I want to filter out null elements can I call List<K> tranformedList = originalList.Select(x =>...

LINQ to SQL

I am finishing off a C# ASP.NET program that allows the user to build their own computer by selecting hardware components such as memory, cpu, etc from a drop down lists. The SQL datatable has 3 columns; ComputerID, Attribute and Value. The computerID is an ID that corresponds to a certain computer in my main datatable of products, the A...

LINQ where Clause doubt

I have tried one where clause in Linq to get details about Users those who are Active and AllowLogin is also true. So how can I compare the table values (both are boolean values) with true or false? ...

Stop LINQ to SQL from executing select statements after insert

I'm using LINQ to SQL to update my database. I'm inserting a lot of records, and when I call SubmitChanges(), LINQ to SQL executes an insert and a select statement for each object. I don't really care to update my objects after they are inserted into the database. Do you know I can prevent LINQ to SQL from issuing the select statement...

Determine if a sequence contains all elements of another sequence using Linq

Given two sets of values: var subset = new[] { 2, 4, 6, 8 }; var superset = new[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; how do I determine if superset contains all elements of subset? I have come up with this: superset.Intersect(subset).Count() == subset.Count() Is this the most logical and efficient method? ...

Linq to SQL: order by value in related table

I have 2 tables which in simplified form look like this: Products( id: int, name: varchar ); ProductSpecs( product_id: int, spec_name: varchar, spec_value: int ); Now I need to sort products (in linq to sql) by value of some specification item (eg. "price"). So I do something like this var products = from p in db.Prod...