linq

Is there a nicer/inline way of accomplishing the following in C# / LINQ?

Often I find myself filling ASP.NET repeaters with items that need the CSS class set depending on index: 'first' for index 0, 'last' for index (length-1), and 'mid' in the middle: _repeater.DataSource = from f in foos select new { ..., CssClass = MakeCssClass( foos, f ) }; privat...

Why does Linq do joins differently

OK this is a little moan but it's also a question. In Linq I can do a join like this: from c in dc.Customers join o in dc.Orders on c.custid equals o.custid ... All well and good and completely rememberable without having to go back and google it. However left joins are far more complicated for some reason: from c in dc.Customers jo...

Localisation/I18n of database data in LINQ to SQL

I have status tables in my database, and "localised" tables that contain language-specific versions of those statuses. The point of the main status table is to define the status ID values, and other meta-data about the status. The "localised" table is to display the text representation in a UI, according to the users' preferred languag...

Extracting text from an IEnumerable<T>

I have a IEnumerable<T> collection with Name, FullName and Address. The Address looks like this: Street1=54, Street2=redfer street, Pin=324234 Street1=54, Street2=fdgdgdfg street, Pin=45654 Street1=55, Street2=tryry street, Pin=65464 I want to loop through this collection and print only those Names, FullNames whose Street1=54 How c...

How do I get the SQL command text when I insert a new record with Linq?

var newUser = new tblUser() { Email = strEmail, Password = strPassword, DateBirth = DateTime.Parse(strDateBirth), }; db.tblUsers.InsertOnSubmit(newUser); db.SubmitChanges(); I want to get the actual sql command text that linq generated. ...

How do I find similar column names using linq?

Hi I am trying to learn Linq, so I am not sure if this can be done. I am working on an import project So I decided to import data using DataSets. My challenge at this point: Having 2 DataTables with different schema, one of which contains my destination schema, and the other one my source schema. What I need to do is perform some...

Linq to SQL vs Entity Framework, Microsoft support for

What are the pros/cons of both? Also, I've heard various rumors concerning if Microsoft will continue to support LINQ to SQL, any further info on this would be appreciated. ...

Can I rely on the order of a SortedDictionary in Linq, and is it doing this efficiently?

When using a SortedDictionary in Linq and iterating over the KeyValuePair it provides, can I be assured that a complex linq query will execute it in ascending order? Here's a brief, although a bit confusing example: Random r = new Random(); //build 100 dictionaries and put them into a sorted dictionary //with "priority" as the key and ...

GroupBy String and Count in LINQ

I have got a collection. The coll has strings: Location="Theater=1, Name=regal, Area=Area1" Location="Theater=34, Name=Karm, Area=Area4445" and so on. I have to extract just the Name bit from the string. For example, here I have to extract the text 'regal' and group the query. Then display the result as Name=regal Count 33 Name=Kar...

Grouping data with sum

I have the following list: mat.Add(new Material() { ID = 1, ProdName = "Cylinder", Weight=23, Discontinued='N' }); mat.Add(new Material() { ID = 2, ProdName = "Gas", Weight = 25, Discontinued='N' }); mat.Add(new Material() { ID = 3, ProdName = "Match", Weight = 23, Discontinued='N' }); I want a result as: 2 Products have Weight 23...

Efficiently retrieving and filtering files.

This earlier SO question talks about how to retrieve all files in a directory tree that match one of multiple extensions. eg. Retrieve all files within C:\ and all subdirectories, matching *.log, *.txt, *.dat. The accepted answer was this: var files = Directory.GetFiles("C:\\path", "*.*", SearchOption.AllDirectories) .Wh...

How to apply a function to an IQueryable instance?

I've begun playing a bit with implementing an IQueryable<T> datatype to query using LINQ. For example I've made a couple of functions like this (It is just a temporary detail that the extension method is not for the specific IQueryable implementation): public static IQueryable<T> Pow<T>(this IQueryable<T> values, T pow) { var e = Bi...

How to return first object of a collection from its parent

I am looking to do the following with a single database query if possible. public class Location { public string URL {get;set;} public IList<Page> Pages {get;set;} } Page firstPage = Session.Linq<Location>() .Where(location => location.URL == "some-location-url") .Select(location => location.Pages).FirstOrDefault(...

Grouping data with Linq or not possibe?

I have a List of concrete objects. Some require to be analyzed to take a decision to which one will be kept (when the group contain more than 1 occurence). Here is a simplified example: List<MyObject> arrayObject = new List<MyObject>(); arrayObject.Add(new MyObject { Id = 1, Name = "Test1", Category = "Cat1"}); arrayObject.Add(new M...

C# Is there a LINQ to HTML, or some other good .Net HTML manipulation API?

Hello all, I have a C# WPF application that needs to consume data that is exposed on a webpage as a HTML table. After getting inspiration from this url I tried using Linq to Xml to parse the Html document, but this only works if the HTML document is extremely well formed (and doesn't have any comments or HTML entities inside it). I h...

How would I use LINQ2XML in this scenario?

I have my LINQ2XML query working half way to my goal: var XMLDoc = XDocument.Load("WeatherData.xml"); var maximums = from tempvalue in XMLDoc.Descendants("temperature").Elements("value") where tempvalue.Parent.Attribute("type").Value == "maximum" select (string)tempvalue; var minimums ...

TransactionScope vs Transaction in linq2sql

What are the differences between the classic transaction pattern in linq to sql like: using( var context = Domain.Instance.GetContext() ) { try { context.Connection.Open(); context.Transaction = context.Connection.BeginTransaction(); /*code*/ context.Transaction.Commit(); catch(Exception e){ con...

LINQ Select Distinct with Anonymous Types

So I have a collection of objects. The exact type isn't important. From it I want to extract all the unique pairs of a pair of particular properties, thusly: myObjectCollection.Select(item=>new { Alpha = item.propOne, Bravo = item...

C#: Anonymous types and property names

Is there any difference at all between this: dataContext.People.Select(ø => new { Name = ø.Name, }); and this: dataContext.People.Select(ø => new { ø.Name, }); ? ...

Joining/merging arrays in C#

I have two or more arrays -- one with IDs, one or more with string values. I want to merge these into a hash table so I can look up values by ID. The following function does the job, but a shorter and sweeter version (LINQ?) would be nice: Dictionary<int, string[]> MergeArrays( IEnumerable<int> idCollection, ...