linq

How do I write a LINQ query that inverts the grouping of a hierarchical data source?

How would one write a LINQ query which takes a hierarchical source data and transforms it so that the grouping is inverted? Say I have a list of Topic objects each of which contains a collection of Tags which represent meta-data tags on that topic. What I need is to write a LINQ query to basically flip the hierarchy inside out so that I...

Populating Childnode list: Linq confusion and separation of concerns

I am trying to implement a tree view in my application. I am using MVC2 Preview 1, and SubSonic 3 SimpleRepository. I am new to both MVC and Linq. My problem is that I am not sure how to add a list of child nodes to the model record that I am passing back to the View. So I have added a IEnumerable called Children to my model class that ...

LINQ to SQL: EntitySet<T>.Assign

I have many-to-many relationship in LINQ to SQL scheme: Products table, Categories table and ProductCategories table (ProductId, CategoryId fields). Product prod; EntitySet<Category> cats = prod.ProductCategories; Now I have a list of category ids (catIds) and I want to set (overwrite) them as categories of a single product in one ope...

SQL Server CLR Trigger with SMO or Linq?

Hello, I've been looking at the possibility of creating a CLR trigger for insert, but every tutorial found gives me examples of using ado.net objects to do the logic - this is not very convenient way..(I don't think I have to convince anyone about that) I was wondering if maybe there is another way for doing this? SMO? Linq? Anything e...

LINQ: When to use SingleOrDefault vs. FirstOrDefault() with filtering criteria

Consider the IEnumerable extension methods SingleOrDefault() and FirstOrDefault() MSDN documents that SingleOrDefault: Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence. whereas FirstOrDefault from MSDN (presumably ...

Populating a list box with variable names from LINQ generated class

I'm quite new to Visual Basic - using Visual Studio 2008 and can't seem to find a way to do the following: I have a few tables in a SQL Server database and have used LINQ to SQL to create classes of those tables. Here's a cut down example of what I'd like: listbox1 filled with table names - APS, SMPS, WCPC, CFLAPS Then from the Select...

.NET DataSet query efficiency

I've got a xml that I'm parsing and trying to extract some data from. Let's say the resulting dataset, after parsing an input xml file, has (2) Tables. Table #1 contains an IP Address and a primary key. Table #2 contains port numbers and a matching primary key. I want to go through both tables and generate an object that contains an I...

LINQ-to-Entities select clause containing non-EF method calls

I'm having trouble building an Entity Framework LINQ query whose select clause contains method calls to non-EF objects. The code below is part of an app used to transform data from one DBMS into a different schema on another DBMS. In the code below, Role is my custom class unrelated to the DBMS, and the other classes are all generated ...

How|Where are closed-over variables stored?

This is a question based on the article "Closing over the loop variable considered harmful" by Eric Lippert. It is a good read, Eric explains why after this piece of code all funcs will return the last value in v: var funcs = new List<Func<int>>(); foreach (var v in values) { funcs.Add(() => v); } And the correct version loo...

Linq - How to turn IQueryable<IEnumerable> into IQueryable

Hi, I have a simple linq statement that isn't quite returning what I would like. I understand why, I just don't know how to wriet it to get what I want. The query is as follows: answers = from a in ents.tblCalls where a.tblSessions.tblUsers.UserID == UserID.Value && (a.StartTime >= sta...

Linq to Xml and Namespace prefixes

I am working with Linq to Xml to manipulate openXml documents. More precisely I am trying to read and write to the documents custom properties. I am currently having a problem appending a prefix onto an XElement. My code looks like: Dim main as XNameSpace = "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" Di...

Hierarchical object retrieve childsIds with linq

How can I do this in one linq statement: If got an list of objects where I have (id, name, parentId). Now I have 3 levels of hierarchy in this list. What I want is that when I'm getting the Id of the top level in my function, I want the Ids of the 3th level. I've got this for the moment: Public List<int> GetIds(int objectId){ var id...

nested subquery in linq to entities

Hi i want to know how to create nested subquery in linq to entities. e.g. this is my sql subquery which i want to translate in linq. SELECT @planned = COUNT(ID) FROM Task_Detail WHERE task_id IN ( SELECT task_id FROM Story_Task WHERE is_testcase = 'true' AND story_id IN ( ...

Convert Session Object to IOrderedQueryable<T>

I need to convert a Session object to an IOrderedQueryable and came up blank. I've thought of creating a wrapper, but its not working properly. Basically, I am pulling a Linq query and would like to store it so that I don't have to pull it each time I visit. There are up to 7-10 parameters per user so it's not something that's great for ...

LINQ - Set element in Dictionary to empty where DateTime value is oldest

I have a Dictionary<string, XMLMessage> where XMLMessage is a struct: private struct XMLMessage { public string Message { get; set; } public DateTime TimeRead { get; set; } } I will use the Dictionary similar to this: storedMessages["1X"] = new XMLMessage() { Message = "<XML>1X</XML>", TimeRead = DateTime....

No datetime values populated from MySQL using SubSonic 3 Linq

I have a MySQL table with a couple of Datetime columns. The columns are set to allow null and some have default value '0000-00-00 00:00:00'. This is a conversion project from ASP to ASP.NET so the table is full of data, and where some rows still have the default value, so I had to set "Allow Zero Datetime=True" in the connectionstring to...

Linq grouping by nullable datetime and using this as criteria

I am struggling with a nullable datetime column [DateInsp] in an ASP.NET app which uses SubSonic3, Linq, MS SQL Server 2005. I had this all working when the datetime column [DateInsp] did not allow nulls. A new requirement forced me to set the [DateInsp] column to allow nulls and now I am struggling getting this piece of functionality ...

How do you dispose of IDisposableobject create inside of a Linq expression?

Linq allows you to create new object inside of a query expression. This is useful when you have classes that encapsulate generation of a list. I’m wondering how you dispose of objects that are created that need it? Example: class Generator { public IEnumerable<int> Gen(int size) { return Enumerable.Range(0, size); }...

LINQ, Skip, OrderBy, and SQL Server 2000

I'm accessing a data context object that is auto-generated by using LINQ to SQL. The SQL database is a SQL Server 2000 box. The class I'm working with is a SQL View. I have a statement that is similar to this: query = _context.OrderDetails .Where(w => w.Product == "TEST") .OrderBy(o => o.DateCompleted) .ThenBy(t => t.LineIte...

Linq Query Returns Incorrect Result Set

I have a very complex Linq to SQL query that returns a result set from a Microsoft SQL Server database. The query is created using syntax similar to: Dim db as MyDataContext = MyGetDataContextHelper() Dim qry = From rslt in db.MyView Select ColumnList If userParam1 IsNot Nothing Then qry = qry.Where(lambda for the filter) End If e...