linq

Converting a generic collection of a concrete type to a collection of a base type

I've got a number of classes that implement a specific interface (ISearchable) and I'd like to return an IEnumerable of the base type (ISearchable) from a static method, but I'm not sure how to convert it without making intermediate collections. The code is pretty simple, one of the domain objects' implementations is like so: public cl...

Check if all items in a Collection have the same value.

an extension method on a collection named MeasurementCollection checks if the property Template.Frequency (Enum) of each item has the same value. public static bool IsQuantized(this MeasurementCollection items) { return (from i in items select i.Template.Frequency) .Distinct() ...

ASP.NET MVC 2 Architecture and LINQ data model

I am creating my very first larger ASP.NET MVC application. After thinking about the application architecture for some time, I thought I would hear some other opinions. I am using LINQ-SQL for my data layer. However, I am not planning on using the LINQ objects throughout the application, since I would like some more specific data types ...

Pivoting a collection of arrays

Basically I have a collection of objects each implement a member of Type IValueCollection public interface IValueCollection : IEnumerable<decimal> { decimal this[int index] { get; set; } } MeasurementCollection.Values is of type IValueCollection. With the logic below I want to pivot a collection of IValueCollection and wrote the...

Composing a where clause in LINQ query at runtime

I'm getting an array of strings for which I want to see if a certain number of data fields in the domain object have all of those strings. I know the data fields at compile-time but I don't know the size of the array at compile-time. Is there a way that I can compose a where clause at run-time so that I can do what I'm looking for in a ...

Iterating through IQueryable with foreach results in an out of memory exception.

I'm iterating through a smallish (~10GB) table with a foreach / IQueryable and LINQ-to-SQL. Looks something like this: using (var conn = new DbEntities() { CommandTimeout = 600*100}) { var dtable = conn.DailyResults.Where(dr => dr.DailyTransactionTypeID == 1); foreach (var dailyResult in dtable) { //Math here, res...

Best practices for dealing with LINQ statements that result in empty sequences and the like?

...I'm a little confused, or unsure about how to deal with errors that arise from LINQ statements. I just love being able to pull one or more items from a collection, based on some criteria... with a single line of code. That's pretty awesome. But where I'm torn is with the error handling, or the boundary condition checking. If I wan...

LINQ Conditional Sum Calculation - cannot be applied to operands of type 'int' and 'bool'

I am trying to perform to calculation. I have a donations (d) table that contains Quantity Needed (d.QtyNeeded) and I need to determine the numbers of items still needed by pulling quantity filled (qtyfilled) from the donors table. Not every donation has a donor, so I need a conditional statement to handle nulls so the sum will work. ...

DB design strategy in Visual Studio

Hi All, I'm currently investigating ASP.NET MVC 2 and LINQ to SQL. It all looks pretty cool. But I have a few application and development lifecycle issues. Currently, I design the DB in SqlServer Management Studio. Then I update my DBML files by deleting and re-importing modified tables. Issues: I can't find how to simply update the...

It is possible to get functionality similar to .NET's LINQ in C++?

It is possible to get functionality similar to .NET's LINQ in C++? Would this require language extensions or could it be done using some very clever macros? Or even through a tool like Qt's moc (meta-object compiler)? Are there any existing LINQ implementations for C++ and if so, what are they? ...

How do you deal with sequences of IDisposable using LINQ?

What's the best approach to call Dispose() on the elements of a sequence? Suppose there's something like: IEnumerable<string> locations = ... var streams = locations.Select ( a => new FileStream ( a , FileMode.Open ) ); var notEmptyStreams = streams.Where ( a => a.Length > 0 ); //from this point on only `notEmptyStreams` will be used/v...

design for table join in .dbml file

hello, I am creating project with MVC vs 2010. I want to use LINQ for queries. I have created a .dbml file and drag and drop tables on it. In some online tutorials there are scalar and navigation properties while dealing with table join in designer surface. How to get these properties, shown in designer surface in .dbml file. Can a...

LINQ to XML: What is the most effective way to move nodes up and down

I need to move sibling nodes before and after certain nodes. Here is the code im working with <tabs> <tab> <name>Overview</name> </tab> <tab> <name>Testing</name> </tab> <tab> <name>Performance</name> </tab> <tab> <name>Braking</name> </tab> </tabs> I woul...

Linq's Aggregate Function, How to make a CSV String

I'd like to make a comma seperated value string with Linq's Aggregate function. Anyone know how to do this? Given an array of strings like this: var authors = new string[] {"author 1", "author 2", "author 3"}; How do I get a single string like this author 1, author 2, author 3? I'm thinking something like authors.Aggregate(author => ...

Reversing Dictionary using LINQ in C#

How to convert Dictioanry<String,List<String>> into Dictionary<String,String> i'm having a dictionary like Dictioanry<String,List<String>>dictOne=new Dictionary<String,List<String>>(); and which containg Key(String) Value(List<String>) A a1,a2 B b1,b2 C c1 i ...

How to check the existence of Attribute in XDocument using LINQ to XML in C#

i'm having an xml file like <Root> <Child Name="A" /> </Root> i need to check whether the "Child" element have "val" attribute.if yes , and if the value is greater than zero then need to change the value of a Boolean variable into true; now i'm using like bool bVal=false bVal=XDocument.Load(Application.StartupPath+"\\foo.xml")...

dot notation equivalent for JOIN

string[] names = { "Burke", "Connor", "Frank", "Albert", "George", "Harris", "David" }; peoples[] people = { new peoples("Connor",20), new peoples("John",22), new peoples("Merry",33), new peoples("Frank",65), new peoples("Frank",34), ...

How to get one level in XML by linq

I have an XML <item id="1"> <item id="1.1"> <item id="1.1.1" /> <item id="1.1.2" /> <item id="1.1.3" /> </item> <item id="1.2" /> </item> <item id="2"> <item id="2.1" /> <item id="2.2" /> <item id="2.3" /> </item> <item id="3" /> I need LINQ to get the first level, without children <ite...

Is it possible to do a linq query on a GridView (ASP.NET)?

Basically I have a datakey that I'd like to query from my GridView instead of looping through all the rows and comparing the key of each row. So I was wondering if it was possible to just do a linq query on the gridview (not datatable) and filter with the datakey. ...

LINQ - selecting second item in IEnumerable

I have string[] pkgratio= "1:2:6".Split(':'); var items = pkgratio.OrderByDescending(x => x); I want to select the middle value and have come up with this. Is this a correct way to select the second value in an IEnumberable? pkgratio.Skip(1).Take(1).First(); ...