linq

Finding Start and End Dates from Date Numbers Table (Date Durations)

I have two tables: a schedule table that contains information about how an employee is scheduled and a numbers table in which each number corresponds to a date. The tables look like: [Employee Schedule] ID Employee ID Project ID Day ID ----------- ----------- ----------- ----------- 1 64 2 168 2 ...

Favorite way to create an new IEnumerable<T> sequence from a single value?

I usually create a sequence from a single value using array syntax, like this: IEnumerable<string> sequence = new string[] { "abc" }; Or using a new List. I'd like to hear if anyone has a more expressive way to do the same thing. ...

Iterating over LINQ entity results

I have what I thought was a very simple piece of code, but the results are baffling me. I'm querying entities using LINQ, and then iterating over the results to create an array. I'm watching the traffic to and from the database, and everything looks good there. When I copy the query that LINQ is sending to SQL and run it directly agai...

Linq to XML Noob question - distinct and order by on attributes

I'm just getting started using Linq to XML and I have a simple document with records like this: <record date="6/27/2002" symbol="DG" price="15.00" /> I want a list of distinct symbols as strings, in order. This gives me an unordered list of all attributes, but I'm stuck var query = from e in xml.Elements() select e.Attribute("sy...

ADO.NET Data Service not dynamically updating

My application is a client/server solution using an ADO.NET Data Service. The Data Service exposes transactionSummaryData as follows: public IQueryable<TransactionSummary> TransactionSummaries { get { return MainForm.transactionSummaryData.Items.AsQueryable(); } } The transactionSummaryData is of type TransactionSummaries whose it...

what is the difference between ObjectQuery and "var" ?

Look please below what is the difference between two type of codes. is there any performance difference or what else? First Codes ObjectQuery departmans = staffContext.Departman; GridView1.DataSource = departmans; GridView1.DataBind(); Second Codes var departmans = staffContext.Depa...

How to use a stored procedure in ADO.NET Entity Framework

I have 3 tables; I write a stored procedure in ADO.NET Entity Framework. ALTER PROCEDURE [dbo].[sp_GetDepartmanData] (@departman nvarchar(50)) BEGIN SELECT d.ID, d.Name as DepartmanName, sb.Salary, sb.email, sp.Name, sp.SurName, sp.Phone, sp.Married, sp.Address FROM Departman d INNER JOIN StaffsBusin...

Modify linq query how to?

How can i shorter below codes than before? i need short and simple method: i dont want to use foreach loop because i have one value. public partial class Test : System.Web.UI.Page { StaffManagementEntities staffContext; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) ...

When to use .First and when to use .FirstOrDefault with LINQ?

I've searched around and haven't really found a clear answer as to when you'd want to use .First and when you'd want to use .FirstOrDefault with LINQ. When would you want to use .First? Only when you'd want to catch the exception if no results where returned? var result = List.Where(x => x == "foo").First(); And when would you want to...

C# Linq statements or a foreach() for totalling subsets?

Which of these solutions is preferred? For a List: List<ExampleInfo> exampleList = new List<ExampleInfo>(); public class ExampleInfo { internal ExampleInfo() { } /* Business Properties */ public int Id { get; set; } public string Type { get; set; } public decimal Total { get; set; } } I wish to get subtotal...

Linq query on the maximum value of 2 fields

Hi, i think the title is a little cryptic, here's what i want to do... i have in my tabel two datetime fields. Now i want to select the rows where the largest (ie the most far in the future) date is larger than 'today' few examples: (today is 6-22) date1: null, date2: null : no match, all lower than now date1: null, date2: 5-31: no m...

linq to sql + stackoverflow exception when querying objects

OK, i have confirmed i only this issue when i attempt to query on the primary key if that primary key in the entity is set to 'Auto Generated Value' -- but w/o this, how can i insert? Sorry if this is a noob linq2sql but i just started working with it. How does one go about using Linq to Sql with this option turned off but also have th...

How can you filter a Deeply Nested XML using LINQ (streaming only), while maintaining the Tree Structure?

Hello I would like to know how to stream over a very large, deeply nested, XML Document using LINQ, while streaming it, filter nodes based on some criteria and then write the streamed output to a file, while maintaining the same original structure of the XML. This should happen without loading the entire document into memory. Is this ...

linq to sql + update table

Heres a newb question for you. I have a multi tier environment so i dont have the origional datacontext where the item was created, thus i am having an issue getting the table to update correctly - here is what im doing: 1.) Get the object from the DAL layer 2.) make changes 3.) call update on the DAL layer and pass the modified ent...

How can I overcome the overhead of creating a List<T> from an IEnumerable<T>?

I am using some of the LINQ select stuff to create some collections, which return IEnumerable<T>. In my case I need a List<T>, so I am passing the result to List<T>'s constructor to create one. I am wondering about the overhead of doing this. The items in my collections are usually in the millions, so I need to consider this. I assume...

LINQ: How to get items from an inner list into one list?

Having the following classes (highly simplified): public class Child { public string Label; public int CategoryNumber; public int StorageId; } public class Parent { public string Label; public List<Child> Children = new List<Child>(); } And having the following data: var parents = new List<Parent>(); var parent ...

to do a substring using LINQ on a string, do I have to correct syntax?

Dim myString As String = "1234567890" Dim Part As String = "" Part = myString.Substring(2, 2) '34 Part = New String(myString.Skip(2).Take(2).ToArray) '34 This code work but the linq one take about 1300% more time than the substring. I have 2 questions Do I have the correct syntax(for LINQ)? Should I stick with the s...

How do I move items from a list to another list in C#?

What is the preferable way for transferring some items (not all) from one list to another. What I am doing is the following: var selected = from item in items where item.something > 10 select item; otherList.AddRange(selected); items.RemoveAll(item => selected.Contains(item)); In the interest of having...

Is there a built-in way to convert IEnumerator to IEnumerable

Is there a built-in way to convert IEnumerator<T> to IEnumerable<T>? ...

Is there a way to check if a Linq to SQL entity has been modified?

The data context can do it, but I need to circumvent the SubmitChanges function for a bit due to having a two stage insert process and not enough time to figure out how to make it work the right way. There are a few things which are lists of items that may or may not be modified, and I'd like to only submit the actually modified items t...