linq

Cast xml to string in LINQ query

Is there a way to cast an xml to a string in a Linq query. Something like this, but in LINQ: select TOP(10) * from PackageSessionNodes where CAST(Interactions as nvarchar(max)) like '%asset id%' order by PackageSessionNodeId desc This doesn't work: var packageSessionNodes = from psn in db.PackageSessionNodes ...

Dealing with spaces in linq to xml

I have an xml file that is like this: <xml>I need this text<notthistext>blahblah<notthistext></xml> I get the text by doing something like this: var mycollection = from myNode in myDoc.descendents("xml") select new { test = Convert.ToString(myNode.Nodes().First())} but the problem is that there are carrige returns and spaces that d...

Linq to SQL: Using int? as a parameter in a query....

So I have a query that looks something like this: var clams = from clam in Clams where clam.ClamTypeID == 143 && clam.ParentClamID == null select clam; Nothing too crazy, returns the results that I need. But when I have it in a function where I'm passing in the possible null value as a int? I...

Using linq to retrieve pairs of Points from collection?

I have a collection of Points, stored in a PointCollection. I need the points in the collection to draw lines. So, for example, if a point collection has four points, that will be three lines. Example: (1) Point(1,1) (2) Point(2,2) (3) Point(3,3) (4) Point(4,4) If I have a list of points, comprised of the four points referenced abov...

Multiple descendants types linq

I sometimes do this: XElement.Descendants("mynodename"); is there a way to do something like this" XElement.Descendants("mynodename or myothernodename"); ...

How to sort a list<datarow>?

I have a datatable which I will convert to a list<datarow>. How do I sort the list based on some datatable column? I think it's something like list = list.Sort(p=>p.Field() but I am not sure about the syntax. I am interested in using LINQ heavily so I was wondering if I should convert the datatable to a strongly typed generic list and t...

Is there a way to use LINQ against Excel?

I am interested in using LINQ against Excel. Right now I am using oledb to read a sheet into a datatable and then maybe convert it to list and then I want to manipulate the data and write back to Excel. Seems cumbersome. Is there an easier way using LINQ to do this stuff in a more straightforward way? ...

How to use LINQ to filter a list of items in another list and not in a third

I'm just getting my feet wet with LINQ. Given three lists of items, this is what I've come up with to only show ClassA items that are referenced in the list of ClassB items but not in the list of ClassC items. var uniqueClassAIDsInClassB = (from classB in classBList select classB.ClassAID).Distinct(); var uniqueClassAIDsInCla...

linq to sql syntax different but should get the same results

I was playing around with expression trees and various Linq syntax. I wrote the following: using (NorthwindDataContext DB = new NorthwindDataContext()) { DataLoadOptions dlo = new DataLoadOptions(); // Version 1 dlo.AssociateWith<Customer>(c => c.Orders.Where(o => o.OrderID < 10700).Select(o...

Memory exception while XDocument.Save()

I am trying to save an XDcoument to a thumb drive which doesnt have enough memory space available. (This is a special test condition for the app) Though the application is giving an exception like below, I cant get that in the try catch block around the XDocument.Save(filePath). Looks like it is a delayed throw. Is it a LINQ issue or am ...

Best way to compare two large string lists, using C# and LINQ?

I have a large list (~ 110,000 strings), which I need to compare to a similar sized list. List A comes from 1 system. List B comes from a SQL table (I can only read, no stored procs, etc) What is the best way to find what values are in list A, that no longer exists in list B? Is 100,000 strings a large number to be handled in an array...

Complex Subtotals needed for in house report

I need a query that returns subtotals by MemberName(PersonID) but broke out into the 2 different ContactTypes(11 & 12 under IsFaceToFace). The below query gets me the base data I need without any subtotals. I tried messing around with WITH ROLLUP and PARTITION BY but those are new to me and never worked completely right. I am sure I...

Nested Linq Queries Saga....

So I am trying to write something like this: SELECT s.CompanyID, s.ShareDate, s.OutstandingShares, s.ControlBlock FROM ( SELECT MAX(ShareDate) AS Sharedate, CompanyID FROM ShareInfo WHERE (ShareDate <= @filter_date) GROUP BY CompanyID ) AS si INNER JOIN tblShareInfo AS s ON s....

Episode 77 - links instead of a for loop

In episode 77 Joel and Jeff were discussing using links instead of a for loop. I looked in Stack Overflow, Google, and Wikipedia but couldn't find any reference to a links construct. The closest thing I could find was a self referencing linked list which would presumably loop indefinitely. Is links a feature of the Links programming l...

Using LINQ to select a random XML node

I'm new to LINQ and am having a problem. I have a file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Galleries> <Gallery ID="10C31804CEDB42693AADD760C854ABD" Title="Test1"> <Description>The first test gallery. Picture of a cat and Wilford Brimley. Can you tell the difference?</Description> <I...

Reorder LINQ result (take 1 row and move it to top) when a condition match

I have a LINQ statement that generate an anonymous type, for example: BookID, AuthorID, [Authors]* Authors return a IEnumerable which also contains many authors, it has 2 columns: AuthorID and AuthorName For example: 1 | 32 | 12, Author 1 20, Author 3 32, Author 19 How can I re-order the Authors object so that [32, Auth...

LINQ: How to Append List of Elements Into Another List

I have the following class public class Element { public List<int> Ints { get;private set; } } Given a List<Element>, how to find a list of all the Ints inside the List<Element> using LINQ? I can use the following code public static List<int> FindInts(List<Element> elements) { var ints = new List<int>(); foreach(var ele...

Help with LINQ Expression

How to write a LINQ Expression (method call syntax preferred) that gives a list of fibonacci numbers lying within a certain range, say 1 to 1000 ? ...

Repository with Specifications for non-linq data access frameworks

I'm currently using NetTiers for all data access operations. While it is easy to use, it is a major pain to deal with template updates and regenerating DAL especially on large and constantly evolving projects. Aside from that, NetTiers is database-centric, not model-centric which is where i'd like to be. What i would like to do is try ou...

In C#, is there a kind of a SortedList<double> that allows fast querying (with LINQ) for the nearest value?

Hi Stackers, I am looking for a structure that holds a sorted set of double values. I want to query this set to find the closest value to a specified reference value. I have looked at the SortedList<double, double>, and it does quite well for me. However, since I do not need explicit key/value pairs. this seems to be overkill to me, an...