linq

LINQ expression to optimize syntax?...

foreach (var item in mainCanvas.Children) { if (item is Button) { (item as Button).Content = "this is a button"; } } Can I use LINQ or other feature of .NET 4 to be more concise (maybe performant)? ...

Linq query problem (too many ado.net request)

Hello! I have trouble in my linq application. I have Orders, OrderChangeLog and OrderItems tables. In 1 query i want to load orders and dependent tables. I use Linq. return from p in _db.dbOrders select new Order { ID = p.ID, OrderStatusChangelog = new List<OrderStatusChangelog>( GetOrderStatusChangelog().Where(x => x.OrderID == ...

How do I OrderBy a DateTime difference in Linq?

I have a table with a CreatedDate column. I want to order by the rows that have been in the database the longest, but need to determine the number of days by subtracting it from DateTime.Now. I realise this is doable simply by ordering by the CreatedDate, but I need to do more than that. I need to order the items by the amount of times...

LINQ Order By to select lowest Index

I am relatively new to LINQ and don't know how to do an Order By. I have an IEnumerable list of myObject and want to do something like select first myObject from myObjectList order by myObject.id asc How can I accomplish this? Thanks ...

Why does this LINQ query compile?

After reading "Odd query expressions" by Jon Skeet, I tried the code below. I expected the LINQ query at the end to translate to int query = proxy.Where(x => x).Select(x => x); which does not compile because Where returns an int. The code compiled and prints "Where(x => x)" to the screen and query is set to 2. Select is never called, bu...

Getting the Average of a Calculated Item With Nullable DateTime

Terrible title, I know... This works and gives me what I'm looking for, but I know there must be a better way to do it: var query = (from a in DB where a.Date.HasValue select a).AsEnumerable(); double avg = (from a in query select (a.Date.Value.Subtract(a.OtherDate).Days).Average(); Basically,...

IQuerable, converting Anonymous Type to Strong Type

Is there a more elegant/concise way of this; I'd like to get rid of foreach loop with the WorkListItem initialization code. var queryable = registrations.Select( r => new { r.Id, r.AccountNumber, r.DateAdded, r.DateUpdated, r.Patient, r.Patient.InsuranceInfos ...

LINQ Lambda - Find all ID's in one list that don't exist in the other list

I have two collections of objects (List list1 and List list2). There is a property on each called "ID". I know that list2 will always have more items than list1, I just need an easy way to get a collection of all the items that exist in list2 but not list1 using LINQ lambda expressions. ...

Can you apply OOP to Linq Projections?

Using Visual Studio 2010 .Net Framework 4 C# Linq to Entities Issue I would like to be able to apply Object Oriented Principles like DRY and SOLID to some Linq Projections. With compiled queries or passed parameters I can apply these to the rest of Linq successfully so far, just not in the projections. Please let me know if this i...

If using LINQ For Queries, Do We Need to Unit-Test Sorting?

Hi Guys, I have the following two methods on an interface for my service layer: ICollection<T> FindAll<T>(Expression<Func<T, bool>> predicate) where T : Post; ICollection<T> FindAll<T,TKey>(Expression<Func<T, bool>> predicate, OrderingOptions<T,TKey> orderingOptions) where T : Post; They are extremely similar. The first one (basica...

Subsonic Single WHERE clause

Is it possible to apply a WHERE clause on a SubSonic query? For example, I get get a single based on id... db.Single<Storage>(id); But how can I get a single based on a simple WHERE clause? db.Single<Storage>(WHERE columnname == "value"); ...

C#: How to manipulate List<String> using LINQ or LAMBDA expression

i'm having a List<String> like List<String> MyList=new List<String> { "101010", "000000", "111000" }; I need to create a new list (List<String>) with "MyList" . so the rows in the "MyList" becomes columns in the new List and the columns become rows So the result will be like { "101", "001", "101", ...

Linq to Entity Framework (v1)

Hi - I'm currently working on a solution, where there are the concepts of users, work queues and work items. At a basic level a user has a series of work queues assigned to them, the work queue containing the work that is to be done. So - at the moment we have a basic Linq query that returns all of the queues, but I was hoping to res...

How to produce many agregations over the set of rows

I'm having difficuly mapping a simple T-SQL query such as this select min(price) as MinPrice, max(price) as MaxPrice, avg(price) as AvgPrice from titles to a Linq Expression such as this: var answer = from t in Titles select new { MinPrice=Min(t.Price), MaxPrice=Max(t.Price), AvgPrice=Avg(t.Price)}; Obviously this doesn't work sinc...

How can I group a List<K> by a List<T>, depending on the results of an expression<T, K>?

I'm having a difficult time expressing this problem, so bear with me and please feel free to ask followup questions. I have a Period object, and a function elsewhere that looks like this: public static bool PeriodApplies(Period period, DateTime date) This function takes an object that describes a recurring period (think a schedule sh...

LINQ to sql insert data c#

I have created one function whose purpose is to add data to database (I'm using linq to sql) This is the code I have written public static void Add(string Name, int Quantitty) { DataDataContext Database = new DataDataContext(); tests test = new tests(); test.Name = Name; test.Quantity = (short)Q...

How to get something like sql '%test%words%' in linq

I know how to use .Contains for looking up '%testwords%', my question is about how to use linq to get '%test%words%'. Thanks ...

Convert Resultant Values to List of Strings in Linq

I'm having issues with returning a list of strings of the .Value of a Linq query: Dim details = <Details> <Vector size="5"> <Item>Syntactic Structures</Item> <Item>Introduction</Item> <Item>The Independence of Grammar</Item> ...

LINQ selection by type of an object

Hi, I have a collection which contains two type of objects A & B. Class Base{} Class A : Base {} Class B : Base {} List<Base> collection = new List<Base>(); collection.Add(new A()); collection.Add(new B()); collection.Add(new A()); collection.Add(new A()); collection.Add(new B()); Now I want to select the objects from the collection...

linq to entities case sensitive comparison

This isn't a case-sensitive comparison in Linq to entities: Thingies.First(t => t.Name == "ThingamaBob"); How can I achieve case sensitive comparison with Linq to entities? ...