linq

Tool for Querying data at Runtime with LINQ

The more and more I use the LINQ the more and more I really enjoy it, and it's syntax. I do a lot of data comparisons on a day to day basis. What I would really enjoy is a tool that allows me to load a DataSet, and then query that DataSet at runtime with LINQ queries. I primarily just want a tool, and to be able to load my DataSets dynam...

Help using LINQ and XML with Enums

I am using C# with the .NET 3.5 framework to write an application. I have an enum like this, public static enum SettingOneType { FooA, FooB, FooC } I also have an XDocument that I load like this in a Load() method, LoadXML(){ ... XDocument SettingsDocument; if(File.Exists(path) { XElement SettingsElement = new X...

LINQ lamba expression to replace multiple characters in a string?

Hi, Is it possible to write a lambda expression that will iterate through array of objects and replace all occurences of 'X', 'Y', ' ' and 'Z' in one of the properties? E.g. return query.Select(x => { x.SomePropertyName= x.SomePropertyName.Trim().Replace(' ', "_"); return x; }).ToList(); For some reason, a query above doesn't repla...

Why is this Linq to XML query not working

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { XDocument xDocument = new XDocument( new XElement("BookParticipants", new XElem...

Dynamic Where in Linq to SQL

How would I get something like this to work so that I can dynamically alter the where-clause in this linq to sql query? Dim AccountID = 1234 Dim AccountList Select Case Types Case 1 AccountList = (from a in dc.Accounts where a.ID = AccountID) Case 2 AccountList = (from a in dc.Accounts where a.ID = AccountID An...

Combine Elements in a List based on Type and Summate their Values, LINQ

Given this structure.. I basically want to be able to take a list of items with multiple types, and create a new list that condenses down the sum of the values of each like-type. However the names of the types are dynamic (they may or may not have a specific order, and there is no finite list of them) using System.Linq; using System.C...

LINQ Comparing Two Lists - Add new, remove old, leave the ones in common

I have two lists (L1,L2) of an object A, L1 is used to store the list of objects(many to many relationship) before they are changed. L2 is the relationship after it has been changed. I need to keep the common elements but add the new ones and remove the ones that aren't in L2. I was wondering if there was a one liner I could use with LIN...

AndAlso in VB Linq query, doesn't seem to be working.

The below query fails with Null Reference Exception when there are elements in BOMIDs where MatID property is nothing. I thought the 'x.MatID isnot Nothing AndAlso' would prevent the x.MatID.Process part of the where from being executed. There are a couple elements in the BOMIDs collection that where MatID is nothing. Any thoughts? Fro...

Delete Cascading Issues with Nhibernate/Linq

Im not so sure if this is a problem with nihibernate or linq(i would assume more linq but i may be wrong) I Have website about boxing. There are three tables in this example Boxer (BoxerId among other properties) Boxer_Match (holds a boxerId and MatchId - which is a composite key) Match (MatchId among other properties) Now currently i...

Anonymous type and intersection of 2 lists

public class thing { public int Id{get;set;} public decimal shouldMatch1 {get;set;} public int otherMatch2{get;set;} public string doesntMatter{get;set;} public int someotherdoesntMatter{get;set;} } List<thing> firstList = new List<thing>(); List<thing> secondList = new List<thing>(); firstList.Add( new thing{ Id=1,shouldMatch1 = 1.11M,...

Merge content of two tables with LINQ-to-SQL

I have two tables in my database Table:Documents Id (int), DocName (nvarchar) ---------------------------- Table:AccessLogs Id (int), DocId (int), AccessTime (DateTime) ---------------------------- How can I write a LINQ query that returns the last 10 accessed documents and fills in the access time from the accesslogs table? I have m...

How can I detect "missing" elements in an IEnumerable<T>?

I've got an IEnumerable<T> containing a list of data elements with consistent intervals in one of the properties: List<Interval> list = new List<Interval> { new Interval{ TIME_KEY = 600}, new Interval{ TIME_KEY = 605}, new Interval{ TIME_KEY = 615}, new Interva...

linq to dataset equivalent of SQL LIKE clause

What is the linq equivalent of a SQL clause like WHERE UserName LIKE 'fr_d'? I'm working with a dataset in memory rather than a SQL database so I don't think I can use something like where SqlMethods.Like(p.UserName, "Fr_d") (Not that my installation of VS2008 is admitting that SqlMethods or even System.Data.Linq.SqlClient exist at the...

LINQ Query That Can Change But Can Group?

This is the sort of thing I want to be able to do. I know you can qury linq results again, but because these results are in groups, I don't know how to. I think the best idea I can think of is to have a query to fetch everything, then do my if statement, then do another query that groups everything (so have 3 separate queries instead o...

Adding elements together across inherited arrays, LINQ

Following up on a previous question, here is my general class structure.. http://stackoverflow.com/questions/3711224/combine-elements-in-a-list-based-on-type-and-summate-their-values-linq I am trying to combine the items using their respective .Add methods, but because of Entity Framework considerations, I can only deal with base types...

Wrapping a complex linq query with a Try-Catch block and catching the correct Exceptions

The below code is a factory class that is delivers objects of type IGraph that have the GraphTypeAttribute implemented. Inside the static constructor of the GraphFactory, a list is built by using Linq to collect the appropriate classes to be delivered by the Factory. Normally without Linq I had a buch of loops and if-then's that could be...

IQueryable: Creating dynamically an OR filtering

I have a set of search criterias in this form: member | value | operator --------+---------+--------- height | 10 | > height | 2 | < name | Carl | == And I want to query all the objects that match any of these criterias. Right now, I'm doing it by: building an expression for each one of the crit...

iterator block to LINQ

I'm having a hard time finding the right LINQ syntax to use for the following iterator block: class Program { class Operation { public IEnumerable<Operation> NextOperations { get; private set; } } class Item { } static Item GetItem(Operation operation) { return new Item(); } static IEnum...

Linq problem: The expression of type 'System.String[]' is not a sequence

Hello, I thought I would throw an omni-search on my data. So I made a function that returns any match on a string. ex. var results = (from d in db.MyData where new string[]{ d.DataField1.ToString(), d.DataField2.ToString(), ... }.Contains(searchTerm) select d); But when I try to iterate over it I get The expression of type 'System....

SingleOrDefault() method: what is "a default value"?

I'm testing for the existence of a user record in the following statement: if (fromUser.AllFriends.Where(af => af.FriendUserID == toUserID).SingleOrDefault() == ??? Given the documentation: Returns a single, specific element of a sequence, or a default value if that element is not found. What does the bold text refer to? What the h...