linq

How to get a list of week days in a month?

In this other question it shows how to get all days of a month. I need the same thing, but I only want to list days of week (I want to exclude weekends). How can I get a list of days of a month excluding weekends? ...

How to replace Linq Cast expression ?

Question: I have some code for pgp encryption from here: http://blogs.microsoft.co.il/blogs/kim/archive/2009/01/23/pgp-zip-encrypted-files-with-c.aspx It has the below method, using some LINQ. I'm still on .NET 2.0 and can't switch higher, yet... How can I replace this expression with ordinary code? I don't really understand Linq, I g...

Split a collection into parts based on condition with LINQ?

I want to achieve something similar to this. But I don't know in what manner I can use that solution. My entity has these properties CustomerName Date SortOrder I've whole list of this Entity. What I want to do is, group all those items in List<> which have consecutive SortOrder and same Date and same CustomerName Example input v...

LINQ request...

How to do this? int[] mas={1,2,3,4,...,n} public var method1(mas) { var d = from i in Object where i.number==mas[0] || i.number==mas[1] || i.number==mas[2]|| ... || i.number==mas[n] select i; return d; } ...

Is there a way of using orderby in a forloop C#?

Hi I have a for loop where i want to orderby the name alphabetically a b c d looking how to do this, wondered even if i could use linq orderby inside the forloop? ...

How to order a list

Hi, I have a function in sitefinity that returns a list of categories. //return list of categories private IList<ICategory> GetCategoryDataSource() { var cntManager = new ContentManager(CaseStudyManager.DefaultContentProvider); IList allCategories = cntManager.GetCategories(); List<ICategory> filteredList =...

How to convert stored procedure's output to collection of anonymous class instances?

I have a lot of pre-linq C# 1.0 code which converts the output from a stored procedure (SQL 2005) into a collection of objects. It goes as follows: Declare a class with properties matching the columns in the output. using SqlDataReader, loop while there are rows to read For every row, add an object to a List return that list Although...

LINQ to SQL Return Type for Execute method in Stored Procedure

I have a stored procedure that builds a dynamic where clause and executes the SQL statement. It looks similar to this: declare @customerId int declare @sql varchar(100) set @customerId = 12 set @sql = Replace('select customerName from customer where customerId = @customerId', '@customerId', @customerId) exec @sql I know the above do...

When to prefer joins expressed with SelectMany() over joins expressed with the join keyword in Linq

Linq allows to express inner joins by using the join keyword or by using SelectMany() (i.e. a couple of from keywords) with a where keyword: var personsToState = from person in persons join state in statesOfUS on person.State equals state.USPS select new { person, State = state.Name }; foreach (var item in per...

How to augment anonymous type object in C#

Hi, I often need to augment a object with a property for instance. Until now (tired of it ;) and it's ugly too) I have done it this way: var someListOfObjects = ...; var objectsWithMyProperty = from o in someListOfObjects select new { o.Name, /*...

date difference in EF4

i need to get a difference of two dates ,one date from a table and one the current date, and the difference should be less than 9 0days. i need to use this as filter in where clause of the linq i tried doing this var list = from p in context.persons where ((p.CreateDT).Subtract(DateTime.Now).Days < 90) select p; i...

Passing anonymous typed group to a function

I needed to pass a IGrouping on an anonymously typed index to a function. List<DataClass> sampleList = new List<DataClass>(); var groups = sampleList.GroupBy(item => new { item.A, item.B, item.C }); I needed to process each of the groups with a function. So I wrote this which works. static void ProcessGroup<T>(IGrouping<T, DataCl...

How to do WHERE IN in linq

So I have a Blog object which has a list of tag objects (List<Tag>). I'm trying to create a method that takes a list of tags and returns a list of blogs that contain all the tags in the passed in list. I was able to make a method that will return a list of blogs if it matches one tag, but not a list of tags. to do that I have this en...

Inverse list ....

In an interview question, I saw this. How inverse the content of this list without loose the references. List<string> list = new List<string>() {"AA", "BB", "CC" }; Update : The question is more like this, you have : public void ReversedList<T>(IList<T> listToReverse) { // To be implemented } sample the list has 1,2,3,4 and mus...

How do I get this result using LINQ?

I am a beginner to LINQ and I have a query where I need result like this. Id | RoleName ------------------- 1 | Member 2 | Admin 3 | Manager 4 | Accountant Id | UserId| RoleId | ExpirationDate ------------------------------------- 1 | 1 | 1 | 1/1/2011 2 | 1 | 4 | 1/1/2012 3 | 2 ...

Create Dynamic LINQ BinaryExpression using data in related tables

Hello, I have an application where all queries are created dynamically based on a simple data message received by a WCF service. A data message is, put simply, a collection of columnname/column value pairs, with the addition of an operator, e.g. Equals, Less Than, etc. Simple Data Message of ColumnName-Value-Operator Name, Joe, Equal...

How can I store and retrieve data from a checkboxlist?

SQL Tables Listing ID, Title..... ListingType ID, Name ListingMatrix ListingID, ListingTypeID Basically a listing can be more than 1 type and I want that to be able to be shown using the ListingMatrix table. However, I'm having a lot of issues populating the checkboxlist because I have it being sorted by Title to keep it user frie...

Add values to object in LINQ to SQL expression

I have this contact list which I'm building using LINQ to SQL. The query to get a list of contacts is: return db.Contacts.ToList(); In the list I also want to display each contact's primary e-mail address. To do this I first rewrite my query: return (from db.Contacts select c).ToList(); I found this nice way to do left join...

Dynamic LINQ with Data Objects

So I have been searching for a simple example to hopefully a simple problem. I have a simple object a List of ListTest (List) public class ListTest{ { public string perName { get; set; } public string perSex { get; set; } public ListTest(string pName, string pSex) { this.perSex = pSex; this.perName = pNa...

C# Linq Weighted Average Based on Date

I've found several posts detailing how to perform a weighted average based on a foreign key, but I have yet to find a solution that deals with my situation. Here it is: I have two tables, table A and a table B many-to-many table linking them; nothing complicated: TableA { A_ID, Other stuff } TableB { B_ID, Date ...