linq

How can I get a randomized collection out of linq-to-sql model?

What's the right syntax for this? var words= from h in db.Words orderby(a => Guid.NewGuid()).ToList()) //error select h; var words= from h in db.Words orderby((a => Guid.NewGuid()).ToList()) //error select h; var words= from h in db.Words orderby...

How do I refactor a common LINQ subquery into a method?

I'm struggling to come up with the right words to summarize this problem, so any input on what I can add to clarify it would be appreciated. The basic scenario is this: I have a basic CMS (with pages, users, etc.). Page is a LINQ data object, which maps directly to a Page table. I've added a method to the Page class called GetUserPerm...

Get maximum amount of recurrences from XML using LINQ

I have an xml file containing basic information about products, with the following structure: - products - Id - Price - ManufacturerId And another one, containing data about manufacturers: - manufacturers - Id - Name I'd like to get the top 3 manufacturers with the most products (manufacturer name and number of ...

IQueryable<T> first free spot

What's the most efficient way to get a first free spot from sorted query returning int collection ? eg.: {1,2,3,4,6} | result.: 5 At the moment I am using foreach and counter that compares current value from sorted quety.ToList() which takes about 600ms on 100 000 records. ...

Accesing object properties held in List<T> after LINQ group by procedure... any help?

I have a List<T> that is holding several objects called tc of type TwitterCollection. Each instance of tc contains 5 properties. The TwitterCollection class looks like this: public class TwitterCollection { public string origURL { get; set; } public string txtDesc { get; set; } public string imgURL { get; set; } public ...

Linq Distinct based on property of object

Is it possible to get the distinct elements of a List based on a property of the objects in the List? Someting like: Distinct(x => x.id) What's not usefull for me is following: .Select(x => x.id).Distinct() because then I would get back a List<int> instead of List<MyClass> ...

Is there a bug in this code from 101 LINQ Samples on MSDN? (Update: Fixed)

NOTE: Charlie Calvert replied below that the 101 LINQ Samples have now been updated with correct code. The MSDN Visual C# Developer Center has a section called 101 LINQ Samples. I found this through a Bing search. The code for SelectMany - Compound from 1 is: public void Linq14() { int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; in...

LINQ To SQL: Getting primary key id number from Table WITHOUT making a second trip?

hello, how would i get the primary key id number from a Table WITHOUT making a second trip to the database in LINQ To SQL? right now, i submit the data to a table, and make another trip to figure out what id was assigned to the new field (in an auto increment id field). also, seond part of my question is: i am always careful to know th...

Group method not recognized in LINQ query

LINQ-newb having trouble using grouping. I'm trying to query an IEnumerable called dosesWithHighestScore. I'm following this example: http://msdn.microsoft.com/en-us/vcsharp/aa336747.aspx#minGrouped Here's my code: var doseWithLowestVolumeForForm = from d in dosesWithHighestScore group d by d.formulation.form into...

Does LINQ To SQL auto update the LOCAL/CLIENT id column after a SubmitChanges call?

just want to know if linq to sql auto updated the id column of a class (table row object) after SubmitChanges is called inserting a new row to that table, that would be fantastic, would anyone be able to confirm this? ...

populating the treeview with linq

protected void Page_Load(object sender, EventArgs e) { if (_xmlDocument == null) _xmlDocument = XDocument.Load("D:/linqToXml/LINQtoXML/framedtag.xml"); var sample = from Emp in _xmlDocument.Descendants("Emp") orderby (Emp.Element("ReportingTo").Value) descending select new {...

Identity Filter Linq .Where

I need to provide a null where clause that has no effect. Currently I have: f=>{f!=null;} However that doesn't really look right. If I were to Select clients, I use .Select(clients => clients) With my filter I also get a warning about not all code paths returning a result. ...

How to do a full outer join in Linq?

I've inherited a database that wasn't designed exactly optimally, and I need to manipulate some data. Let me give a more common analogy of the kind of thing I have to do: Let's say we have a Student table, a StudentClass table keeping record of all the classes he attended, and a StudentTeacher table that stores all the teachers who tau...

Using Large Arrays in VB.NET

I want to extract large amounts of data from Excel, manipulate it and put it back. I have found the best way to do this is to extract the data from an Excel Range in to a large array, change the contents on the array and write it back to the Excel Range. I am now rewriting the application using VB.NET 2008/2010 and wish to take advantag...

Best way to present the user the XML values of a file ?

Hey Team, I got a XML File, looking familiar to this : <root> <carnumber>12</carnumber> <carcolor>2</carcolor> <cartype>5</cartype> </root> Like you see I got some Elements with values/text in it. The car element for example can take values from 1 to 1000. But the element carcolor can take values from 1 - 5 and the cartype from 1 -...

C# - Use OfType and ignore inherited classes

I have a MenuStrip with lots of items and am trying to have on event they all subscribe to so I am trying to do menuStrip1.Items.OfType<ToolStripMenuItem>(); and for each one I do this: menuitem.Click += new EventHandler(MenuItem_Click); The problem is there is a ToolStripSeperatorItem which inherits off ToolStripMenuItem and that app...

How do you keep your Domain Logic seperate from DB/Persistence Logic with Linq-2-Sql?

I'm trying to get at the best way to seperate the concerns of my domain logic and my persistence logic. I'm using Linq-2-Sql for the data access and I've been following the NerdDinner tutorial. If you look at page 40, you can see they are using partial classes to business rules to their Linq generated classes. To me, that feels wrong (is...

Is there a way to get the sql created by a linq querty?

I've been developing a ASP.NET page and have been using LINQ to handle talking to MS SQL server. I'm ok at basic SQL, however I'm a lot better with designing queries using LINQ. I know they are similar but I find it easer to design complex queries in LINQ. My question is this: Is there a way to design a query in LINQ and then get the SQL...

C# Generics and Reflection

Hi, i'm using linq. All my queries looks like var query = dc.GetTable<myType>(). I wish i could choose "myType" using a string parameter. I tried to create a Type object using reflection, but the compiler doesn't recognize Type objects as class definitions. Any suggestions? Thanks ...

Use generics to make a method work for any data type

I'm not quite sure I'm understanding how I can utilize generics in C# properly. Say I have the following method. I would like to allow it to work on Lists of any type. Currently I have List where Row is a custom struct, I want to reuse this sort method for half a dozen structs that I make. I thought I could just do List<T> in the return ...