linq

Examples of Common Linq Expressions

When I'm writing code, I'm starting to identify those places where I could use Linq. My problem is that I'm still very new to the syntax. I learn best through examples, but I can't seem to easily find the example I need. I wanted to start this thread to create a repository of common Linq expressions that others could stumble upon via ...

Need help with adding Where clauses in loop with Linq

Below is a rough and simplified code example of what I am trying to do and it is not working as expected. I am using Linq to Entity in case that matters. Basically in the first loop with the DateClause's ends up returning nothing if I have two date clauses, works great with one date clause. In the TextClause's loop it seems to ignore...

Convert to list a object with anonymous type

I need convert to list a object with anonymous type because when i databind the gridview i get "The data source does not support server-side data paging" or How i can solve this? object o = HttpRuntime.Cache[key]; if(o is ICollection) { //Sort Object o = ((IQueryable)this.DataSource).AsQueryable().OrderBy(SortExpresion); ...

Elegant way to create a nested Dictionary in C#

I realized that I didn't give enough information for most people to read my mind and understand all my needs, so I changed this somewhat from the original. Say I've got a list of items of a class like this: public class Thing { int Foo; int Bar; string Baz; } And I want to categorize the Baz string based on the values of ...

XML serialization and LINQ

I've currently got an XML element in my database that maps to an object (Long story short the XML is complicated and dynamic enough to defy a conventional relational data structure without massive performance hits). Around it I've wrapped a series of C# objects that encapsulate the structure of the XML. They work off a base class and th...

Conditional where with or criteria linqtosql

Howdy, I've figured out how to do conditional queries with linq to sql and I've also figured out how to OR where clauses. Unfortunately I can't figure out how to do both at once. I can do a conditional where clause something like: var ResultsFromProfiles = from AllPeeps in SearchDC.aspnet_Users select AllPeeps...

Another Linqification question

Say I have a List of objects, and the object has a string property. I want to get a single comma-separated list of the value of each string property of each object in the list. Here's 1 way to do it (sans linq) StringBuilder result = new StringBuilder() foreach(myObject obj in myList) { result.Append(obj.TheString); result.Append...

rss feed by linq

I am trying to extract the rss feed using linq. Thought it would be simple, but its is not returning any nodes. probably i have to go the channel/item node, but don't know how. Dim rssUrl As String = "http://webclip.in/rss.aspx?u=mostliked" Dim rssDoc As XDocument = XDocument.Load(rssUrl) Dim rssResultSet = From node In rssDoc.Descenda...

Get table-data from table-name in LINQ DataContext

I need to get table data from table name from Linq DataContext. Instead of this var results = db.Authors; I need to do something like this. string tableName = "Authors"; var results = db[tableName]; It could be any table name that is available in DataContext. ...

Add single element to a sequence for an expression

Hi there. Imagine you would want to select all elements of one sequence all, except elements contained in sequence exceptions and single element otherException. Is there some better way to do this than? I'd like to avoid creating new array, but I couldn't find a method on the sequence that concats it with a single element. all.Except(e...

Why would Entity Framework not be able to use ToString() in a LINQ statement?

This works in LINQ-to-SQL: var customersTest = from c in db.Customers select new { Id = c.Id, Addresses = from a in db.Addresses where c.Id.ToString() == a.ReferenzId select a }; foreach (var item in customersTest) { Con...

XDocument comparrison

I have two documents with a simple schema that I need to compare: current doc: <Sections> <Section Number="1"/> <Section Number="2"/> <Section Number="4"/> <Section Number="5"/> </Sections> previous doc: <Sections> <Section Number="1"/> <Section Number="2"/> </Sections> The result of the comparison will be a list secti...

How do I combine LINQ expressions into one?

I've got a form with multiple fields on it (company name, postcode, etc.) which allows a user to search for companies in a database. If the user enters values in more than one field then I need to search on all of those fields. I am using LINQ to query the database. So far I have managed to write a function which will look at their inpu...

How to blank out a field in an MVC app using TinyMCE

I've got an MVC app that gives the user textarea's to update some description fields. It's strongly-typed to a table object, and the fields are wrapped in a form with a Submit button. Occaisionally they don't want any data in a field, but when they delete the text and try to save, the blanked-out field comes back with its original text ...

How to specify to Regex (in a LINQ Query) to only compare the start of a string?

I have the following code: string filterTerm = txtDocFilterTerm.Text.ToLower(); var regEx = new Regex(filterTerm); //griQualifiedDocs is a grid //storage.QualifiedDocs is the master/original collection griQualifiedDocs.ItemsSource = storage.QualifiedDocs .Wher...

Generating XMl from DataContext

What is the modification needed in the following code to get XML from DataContext? DataClasses1DataContext dc = new DataClasses1DataContext(); var query=new XElement("Numbers", from p in dc.Pack select new { ...

How to get parent and only one child node

Lets say I have this xml: <categories> <category text="Arts"> <category text="Design"/> <category text="Visual Arts"/> </category> <category text="Business"> <category text="Business News"/> <category text="Careers"/> <category text="Investing"/> </category> ...

Get more returns of a Stored Procedure (Linq to SQL)

Hi everybody. I have a question. Please it´s extremely urgent !! I created a Store Procedure to make tests for study its functionality. my procedure execute two selects: Example: Select TOP 20 * From NotaFiscal Select TOP 20 * From ProdutoNotaFiscal Using the ADO.NET, the Dataset is filled with 2 results and generates 2 DataTables...

Linq, Map List to CheckboxList

If I have a List(Of Guid) can I map those Guid's with a single Linq expression to a CheckboxList of values? ...

LINQ to XML: filter a query using XElement.Attributes() collection with both XName and Value

Using LINQ to XML, XElements and XAttributes I'd like to filter a query using an exact match on an XElement's Attributes() IEnumerable collection of XName/Value pairs, against a provided IEnumerable Attributes collection of XName/Value pairs. <container> <!-- logical group of settings --> <group name="group-one"> <!-- setti...