linq

Normalize data with LINQ

Assume we have some denormalized data, like this: List<string[]> dataSource = new List<string[]>(); string [] row1 = {"grandParentTitle1", "parentTitle1", "childTitle1"}; string [] row2 = {"grandParentTitle1", "parentTitle1", "childTitle2"}; string [] row3 = {"grandParentTitle1", "parentTitle2", "childTitle3"}; string [] row4 = {"g...

How to Group Multiple Items using GroupBy()?

Consider the following simplified objects and relationships: public class Job{ int JobId; String name; String status; Discipline disc; WorkCategory workCat; } public class Discipline { int DisciplineId; String DisciplineName; } public class Workcategory{ int WorkCategoryId; String WorkCategoryName; } public Class ...

C# - Convert Implict Type to ObservableCollection

Hello, I have a LINQ statement that returns an implicit type. I need to get this type to be an ObservableCollection in my Silverlight 3 application. The ObservableCollection constructor in Silverlight 3 only provides an empty constructor. Because of this, I cannot directly convert my results to an ObservableCollection. Here is my code: ...

Can we make this more terse with linq?

I am curious if there is a way where I can capture the lower lines (the creation of the dictionary and loop to add values) in the linq statement itself. Right now the select new returns a new anonymous type but I am wondering if there is a way to make it return a Dictionary with all the values pre-populated. XDocument reader = XDocu...

Can I get the delta of two IEnumerables in LINQ?

Not the aggregate delta but the delta of each element. Here is some code to explain what I mean: var deltaTotals = _deltaEnumerable.Select(a => a.Amount).ToList(); var oldTotals = _totalsEnumerable.Select(d => d.Amount).ToList(); // trigger change in _totalsEnumerable // ** can LINQ do the lines below var newTotals = totalsEnumerable...

Reading the next line using LINQ and File.ReadAllLines()

Hi, I have a file which represents items, in one line there's Item GUID followed by 5 lines describing the item. Example: Line 1: Guid=8e2803d1-444a-4893-a23d-d3b4ba51baee name= line1 Line 2: Item details = bla bla . . Line 7: Guid=79e5e39d-0c17-42aa-a7c4-c5fa9bfe7309 name= line7 Line 8: Item details = bla bla . . ...

NUnit: What is the most concise way to assert whether an IEnumerable contains an object of a certain type?

I have a method named RenderContent which returns object[] In my unit test, I need to assert that this array does not contain any objects of type VerifyRequest At the moment, I'm using the following Assert statement. Is there anything more concise? Assert.That( domain.RenderContent().OfType<VerifyRequest>().Count(), Is.EqualT...

compare the dates with linq

I have this code: i.SpesaVitto = db.TDP_NotaSpeseSezB.Where(p => p.GiornoFine == null && ((DateTime)p.Giorno).Date == ((DateTime)i.Data).Date && p.MissioneID == missioneID).Sum(p => p.Costo); If i launch it i obtain this error: The specified type member 'Date' is not supported in LINQ to Entities. ...

date difference with linq

With this code i.SpesaAlloggio = db.TDP_NotaSpeseSezB.Sum(p => p.Costo / (((DateTime)p.DayEnd).Subtract((DateTime)p.DayStart).Days + 1)); i receive this error: LINQ to Entities does not recognize the method 'System.TimeSpan Subtract(System.DateTime)' method, and this method cannot be translated into a store expression. How can i ...

Is there a plugin for visual studio 2008 that will allow you to use Linq in a watch window?

I am working in Visual studio 2008 and would like to be able to use Linq in a watch window. Is there a tool that will allow me to do that? ...

List<T> Concatenation for 'X' amount of lists

I have for example 5 List all of the same type. Can I simply do List<T> newset = List1.Concat(List2).Concat(List3).Concat(List4)..... ...

Where clause in LINQ - C#

I have the following which works in SQL Query Analyzer. select oh.* from order_history oh join orders o on o.order_id = oh.order_id where oh.order_id = 20119 and oh.date_inserted = ( select max(date_inserted) from order_history where order_id = oh.order_id group by order_id ) How would I go about converting to LINQ? From test ...

Finding results in a Enumerable object quickly.

I am trying to write a utility to see if a user has logged in to windows since a date that I have stored in a database. private void bwFindDates_DoWork(object sender, DoWorkEventArgs e) { UserPrincipal u = new UserPrincipal(context); u.SamAccountName = "WebLogin*"; PrincipalSearcher ps = new PrincipalSearcher(u); var res...

Linq2Sql Designer Turns Store Procedure Multiple Result Set to Single

In a linq2sql class, I call a stored procedure that returns multiple result sets and it works. Whenever I add a new procedure in the linq2sql designer and it stealth converts the aforementioned stored procedure from IMultipleResults to ISingleResult in designer.cs. I replaced the code with a previous version and it works, but why do...

Asp.net User Login Count and LastLogin

Is the normal way to do this to just make 2 cols in my User table, loginCount and lastLogin, and then when a user is Authenticated hit the db and update them in the code behind? I am using OpenID only. Is there a better way? ...

When using LINQ shall we use 3 layers?

When using LINQ to SQL or Entity framework,shall we need to separate application in 3 layers?BLL,DAL,Interface? ...

Array Intersection Based Upon A String Comparison

I've got two arrays where I want to find all the elements in Array0 where the full string from Array1 is contained in the string of Array0. Here is the scenario: I've got a string array that contains the full path of all the xml files in a certain directory. I then get a list of locations and only want to return the subset of xml file...

what LINQ query will return results based on partial matches against arbitrary number of an entity's fields?

consider the following code, which represents an attempt to implement partial matching. the intended result is a row for any 1 or more fields that match between the query entity and the data store. so if you supply person.email we want a match against that, if you supply person.email and person.FirstName we should filter the results fur...

linq include with condition

i have function public Menu Details(int? id) { return _dataContext.Menu.Include("ChildMenu").FirstOrDefault(m => m.MenuId == id); } now i need to add condition to child list ChildMenu something like fieldname=id. how can i do it? ...

Is there any work-around for NHibernate-to-LINQ not supporting subqueries?

I know LINQ-to-NHibernate currently does not support sub-queries (http://ayende.com/Blog/archive/2009/07/26/nhibernate-linq-1.0-released.aspx). Is there any workaround about it? ...