linq-to-objects

Linq returning IEnumerable<T>

I wish to return enumerable items in order to bind the nested grid. Top grid displays Book Title and the nested grid displays the authors list of that book. Author Collection static public Author[] Authors = { new Author {FirstName="Johnny", LastName="Good"}, new Author {FirstName="Graziella", LastName="Simplegame"}, new Au...

LINQ query to detect duplicate properties in a list of objects

I have a list of objects. These objects are made up of a custom class that basically contains two string fields (String1 and String2). What I need to know is if any of these strings are duplicated in that list. So I want to know if "objectA.String1 == objectB.String1", or "ObjectA.String2 == ObjectB.String2", or "ObjectA.String1 == Objec...

LINQ Select: different projects same code different results

The same code on two different web sites (on the same solution), VB.Net (framework 3.5). The Code: Public Class UserTest Public hhh As Integer Public fff As String Public Sub New(ByVal hh As Integer, ByVal ff As String) Me.hhh = hh Me.fff = ff End Sub End Class Dim lst As List(Of UserTest) = N...

convert ienumerable linq list to typed list

Hi, I'm having the following issue: I'm using linq to filtrate some data in this way: var listPerson = (from objPerson in ListPerson select new { objPerson.IdPerson, objPerson.ApePerson, ...

VB.net Linq Expression from ListItemCollection to List(Of Guid)

I want to do something like this: Dim selectedCourses As List(Of Guid) = From item In chkListCourses.Items Where item.Selected = True Select item.Value but I get the error: Unable to cast object of type 'WhereSelectEnumerableIterator2[System.Object,System.Object]' to type 'System.Collections.Generic.List1[System.Guid]'. ...

Linq query with a Join, not returning the correct results.

Hi Folks, i've got the following code, which compiles but doesn't retrieve the correct results. I'm trying to retrieve all the Banned log entries for people who have been recorded at cheating on a gaming server. The database (in this case, two IList tables) has two simple tables. GameFiles : the game which has a log file .. which w...

How can I use .Contains() to get at a property of a collection using LINQ to Objects?

I have an IEnumerable of the following object: public class Recipient { public int UserID { get; set; } public string Name { get; set; } } So, I have an IEnumerable<Recipient> Recipients, and I'd like to do a .Contains() on Recipients. Except, I'd like to do a .contains() on each recipients UserID, to see if my Recipien...

Learn LinqToSql or stick with ADO.NET?

I'm debating what technology to use for an upcoming ASP.NET project. Assumptions: I will be using Visual Studio 2008 SP1 (.NET Framework 3.5) The back-end will be a SQL Server 2005 database (or possibly 2008) Code will be written in C# I already have experience with LinqToObjects and LinqToXml I also have experience with ADO.NET and s...

How might I complete this example using LINQ and string parsing?

I'm trying to write a simple program that will compare the files in separate folders. I'm currently using LINQ to Objects to parse the folder and would like to included information extracted from the string in my result set as well. Here's what I have so far: FileInfo[] fileList = new DirectoryInfo(@"G:\Norton Backups").GetFiles(); v...

How can I iterate over a collection and change values with LINQ extension methods?

Lets say I have a collection of Messages which has the properties "UserID" (int) and "Unread" (bool). How can I use LINQ extension methods to set Unread = false, for any Message in the collection in whose UserID = 5? So, I know I can do something like: messages.Any(m => m.UserID == 5); But, how do I set the Unread property of each o...

How to select static text with LINQ query?

I have a listbox and I want its ItemsSource to be all the items of an ObservableCollection + an additional static text such as "All Items." Right now I just have something similar to: listbox1.ItemsSource = from Car c in Cars select c.Model I know I could just manually add the text to the listbox, but I want it...

Get a list of distinct items and their count

I have an object, that has many properties but the only two to worry about are: myobject.ID which is an int myobject.Names which is a HashSet Then I have a List of those objects that looks something similar to this: List<myobject> I use Linq to get a some of the data into a repeater, but I'm not sure how to get the list of Names and...

Orderby a HashSet in an Object in Linq

I have a pretty complex Linq statement with multiple joins, but am having a problem with an orderby statement. I think I can reduce the question down a bit, but will expand if needed. First my object has the following properties: myObject.ID This is a basic Int myObject.BrandName This is my HashSet<string>. I need to sort my Object ...

Dynamic where clause in Linq

I know there are a lot of examples of this on the web, but I can't seem to get this to work. Let me try to set this up, I have a list of custom objects that I need to have limited on a range of values. I have a sort variable that changes based on some action on the UI, and I need to process the object differently based on that. Here...

Linq to objects from Untyped Datatable Unable to cast object of type 'System.Int32' to type 'System.String'.

Hi, I am completely at a loss for why linq is throwing an Unable to cast object of type 'System.Int32' to type 'System.String' exception. The code is part of a generic function meant to pull the DataTextField and DataValueField out of an untyped DataTable and place them in a list of KeyValuePair(of string, string). I added a for each lo...

linq extension methods: Is there a way to add an OR clause to a where method

I have a statment like this: foreach (var container in modelEntities. Where(me => me.Value.ModelEntityType == (int)EntityType.Container)) Now I want to add an or clause to this where statement, however I can't seem to be able to do it. Is there a way? or do I have to do the var containers = (from me in modelEntities whe...

IList - LINQ to filter and order by

I have the following test code to search a generic list: public void DoSearch(string searchTerm) { IList<MyEntity> entities = GetCollectionOfEntities(); IList<MyEntity> results = entities.Where(d => d.Description.Contains(searchTerm)).ToList(); } I want to pass an order by parameter (which would be a property of MyEntity) and of co...

Selecting DataRows into new structures using LINQ. Calling Distinct() fails

Consider these two structures: struct Task { public Int32 Id; public String Name; public List<Registration> Registrations; } struct Registration { public Int32 Id; public Int32 TaskId; public String Comment; public Double Hours; } I am selecting a bunch of entries in a DataTable into new structures, like s...

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"); ...

Cannot serialize parameter of type 'System.Linq.Enumerable... ' when using WCF, LINQ, JSON

I have a WCF Service. It uses Linq-to-objects to select from a Dictionary. The object type is simple: public class User { public Guid Id; public String Name; } There is a collection of stored in a Dictionary<Guid,User>. I want to have a WCF OperationContract method like this: public IEnumerable<Guid> GetAllUsers() { ...