linq

Calculating monthly reports or statistics - MVC Entity Framework LinQ

Hi I am using MVC (C#) and the Entity Framework. I have a sql server database with tables called Profile, Gallery, ArtWork : PROFILE: UserName, FirstName, etc... GALLERY: GalleryID, UserName, GalleryName, ARTWORK: ImageGuid, GalleryID, Description, PublishDate, Views, Downloads, I have the following relationship between the tables: ...

LINQ - array property contains element from another array

I have a object (product), with a property of type 'array' e.g. product.tags = {"tag1","tag2","tag9"} I have an array of input tags to filter on. ... but this is not quite working: List<string> filterTags = new List<string>() { "tag1", "tag3" }; var matches = from p in products where p.Tags.Contains(filterTags) select p; Any re...

Returning Index in Enumerable Select

I have a List<MyClass> with 2 items which have a SequenceNumber property. If I use this code below the returned index is 0 not 1: var test = TrackingCollection .Where(x => x.SequenceNumber == 2) .Select((item, index) => new { ...

LINQ VB.NET variable not found when looping through grouped query

I'm trying to do the following LINQ grouping, which works in the debugger (the results are populated in the GroupedOrders object. But VS 2008 gives me the following error at design time... Name 'x' is not declared Dim GroupedOrders = (From m In thisConsultant.orders _ Group m By Key = m.commCode Int...

Can someone help me refactor this C# linq business logic for efficiency?

I feel like this is not a very efficient way of using linq. I was hoping somebody on here would have a suggestion for a refactor. I realize this code is not very pretty, as I was in a complete rush. public class Workflow { public void AssignForms() { using (var cntx = new ProjectBusiness.Providers.ProjectDataContext())...

Read Parent nodes only from XML using LINQToXML

I have XML string that has parent nodes "Committee" and inside that another child node "Committee" is there. When I am using "from committee in xDocument.DescendantsAndSelf("Committee")" it is reading childnode also, but I don't want to read child nodes, I just want to read Parent nodes only. <Committee> <Position>STAFF</Position> ...

Simple Linq Dynamic Query question

In Linq Dynamic Query, Scott Guthrie shows an example Linq query: var query = db.Customers. Where("City == @0 and Orders.Count >= @1", "London", 10). OrderBy("CompanyName"). Select("new( CompanyName as Name, Phone)"); Notice the projection new( CompanyName as Name, Phone). If I have a class like this: public class Co...

Using Linq, how to separate a list in to grouped objects by name?

I have a table where a record looks like this varchar(255) Name varchar(255) Text varchar(255) Value Name is the DDL name, Text is what is displayed, and Value is returned upon selection. There are between one and twenty options for each Name. Without iterating though each option like a cursor, is there any way to pull out a li...

Is there a way to transfrom a list of key/value pairs into a data transfer object

...apart from the obvious looping through the list and a dirty great case statement! I've turned over a few Linq queries in my head but nothing seems to get anywhere close. Here's the an example DTO if it helps: class ClientCompany { public string Title { get; private set; } public string Forenames { get; private s...

What's the convention for extending Linq datacontext with set based helper operations specific to one collection of entities only

Hi All I might be vaguing out here but I'm looking for a nice place to put set based helper operations in linq so I can do things like; db.Selections.ClearTemporary() which does something like db.DeleteAllOnSubmit(db.Selections.Where(s => s.Temporary)) Since I can figure out how to extend Table<Selection> the best I can do is crea...

What's elegant way to rewrite following LINQ statement using extension methods?

I have following LINQ statement and I want to rewrite it using extension methods. from x in e from y in e from z in e select new { x, z } One possible solution is: e.Join(e, x => 42, y => 42, (x, y) => new { x, y }) Join(e, _ => 42, z => 42, (_, z) => new { _.x, z }); However this is everything but elegant. Do you any idea how...

How can I use external expressions in Linq with EF4 (and LINQKit)?

I want to separate out often used expressions in linq queries. I'm using Entity Framework 4 and also LINQKit but I still don't know how I should do it the right way. Let me give you an example: Article article = dataContainer.Articles.FirstOrDefault(a => a.Id == id); IEnumerable<Comment> comments = (from c in article.Comments wher...

How to update single object in sql to linq

For example a = datacontext.customers.FirstOrDefaul(); b = datacontext.customers.Skip(1).FirstOrDefaul(); a.name="name1"; b.Name="name2"; When i call datacontext.SubmitChanges(), Two object updated. I don't want this. I need only Update a object. How do it? EDIT WPFWindow windowA=new WPFWINDOW() windowA.DataContext=a; windowA.Sh...

linq sort many 2 many question

My main entity is called [Contract]. Contract has a many 2 many relationship w/ [Service]. When I query for a list of Contracts I grab the 1st Service available like this: IQueryable<Contract> q = ctx.Contracts.Skip(startRow - 1).Take(pgSize); q.Select(c => new ContractSearchResult() { ContractID = c.Contra...

Tell LINQ Distinct which item to return

I understand how to do a Distinct() on a IEnumerable and that I have to create an IEqualityComparer for more advanced stuff however is there a way in which you can tell which duplicated item to return? For example say you have a List<T> List<MyClass> test = new List<MyClass>(); test.Add(new MyClass {ID = 1, InnerID = 4}); test.Add(new ...

How to eager load sibling data using LINQ to SQL?

The goal is to issue the fewest queries to SQL Server using LINQ to SQL without using anonymous types. The return type for the method will need to be IList<Child1>. The relationships are as follows: Parent Child1 Child2 Grandchild1 Parent > Child1 is a one-to-many relationship Child1 > Grandchild1 is a one-to...

How can I convert this linq.expression from C# to VB?

[QueryInterceptor("Somethings")] public Expression<Func<Something, bool>> OnSomethings() { // Code here } I had a view guesses, looked on msdn, but there are no examples matching the way that that is used. Ideas? ...

Using LINQ Group By to return new XElements

I have the following code and got myself confused: I have a query that returns a set of records that have been identified as duplicates and I then want to create a XElement for each one. This should be done in one query I think but I'm now lost. var f = (from x in MyDocument.Descendants("RECORD") where ite...

Nhibernate Linq In Clause

Is it possible to get Nhibernate linq to generate a query with an "In" clause? e.g. - Where AnID in (x,y,z)? ...

How to find out if an object's type implements IEnumerable<X> where X derives from Base using Reflection

Give a base class Base, I want to write a method Test, like this: private static bool Test(IEnumerable enumerable) { ... } such that Test returns true if the type of o implements any interface of IEnumerable<X> where X derives from Base, so that if I would do this: public static IEnumerable<string> Convert(IEnumerable enumerable) { ...