linq

Sub Query in LINQ

Hi I have an SQL Query as given below SELECT ui.PageStyleCss FROM UserImages ui WHERE ui.UserImageId IN ( SELECT inv.UserImageId FROM Invitation inv JOIN InviteeEmails invEmails ON inv.InviteID = invEmails.InviteID WHERE invEmails.InviteGUID = @InviteGUID ) How can I write this in LINQ? Thanks ...

LINQ - Minimize Records Returned - Correct Way to Write These Expressions

Employee is a sample entity type. var r1 = (from c in _ctx select c).Skip(5).Take(5); // my intent is to pull the first record from the query var r2 = (from c in _ctx select c).FirstOrDefault<Employee>(); // my intent is to pull the last record from the query. // any good way to ask for the result back in the reverse // orde...

LINQ, use string[] in a query

I have a query that looks something like this... string value return DataContext.Tags.Where(t => t.Keys.Any(k => k.Ring.RingName == category)) .Where(t => t.Keys.Any(k => k.Ring.Keys.Any(c => c.Tag.TagName == value))); It works good, I love it. But it needs to do something extra. the 'value' string will act...

Subsonic Query Construction for Contains(Guid)

I have a "Notes" table. Notes support one level of threading - in other words you can reply to a note but cannot reply to another reply. So the table looks something like the following: CREATE TABLE [dbo].[Notes] ( [NoteId] [uniqueidentifier] ROWGUIDCOL NOT NULL DEFAULT (newid()) CONSTRAINT [PK__Notes] PRIMARY KEY ([NoteId]), [...

Populating List<T> within a foreach loop after pattern matching

Hi, I'm fairly new to C# programming and need some help. I am trying to assign values I've gathered from a JSON feed to my own type, a class in which I have defined certain fields (properties) to put the JSON elements into, plus elements that are derived from a RegEx pattern matching process. This will then allow me to access the objec...

Simple LINQ question - how to iterate through a group?

I need some help with a LINQ query in VB.Net, please. I have this simple group statement: Dim drivers = From d In DriversOwners _ Group d By Key = d.UnitNumber Into Group _ Select Key, DriverGroup = Group This works, and returns me the data I need to work with. Now I want to iterate through the groups, using a For Each construc...

Linq to SQL collections do not populate.

I have a Linq to SQL class. There is a one to many relationship in my database. The relationship maps correctly in the designer, and an EntitySet<> property is created in the designer. When I run the code, the EntitySet<> does not populate with any data, even though there are associated records, they do not populate into the EntitySet...

LINQ is returning class name and not value data

Hi, I have a collection of TwitterCollection objects held within a List. I am populating the TwitterCollection object (tc) via a foreach loop, and then accessing it through LINQ. My class with its properties looks like this: //simple field definition class public class TwitterCollection { public string origURL { get; set; } p...

Linq query help

I have two collections, and need to create a new collection from the two collections. Assume the following class: public class Widget { property int Id{get;set;} property string Label{get;set;} } We have two IList classes. I would like to create an Anonymous type with Id, Label, and Exists So doing this for Id and Label, I ha...

How is the performance of entity framework 4 vs entity framework 3.5?

I have one query on my page that takes at least a half second to execute using EF 3.5. When I used a stored procedure the speed was noticably faster. It is a very complex query. Will there be any performance improvements in the upcoming EF 4.0? And does EF 4.0 really beat out 3.5 performance wise? ...

Linq with EF dynamic search

I am using EF 3.5 with MVC. I want to made a search page, has some fields for criteria like date, int etc. What is the way in linq to entities to filter the result dynamically. If there are one parameter we can use .where(a=>a.id==1) but many combination with optional param how can i load results and then pass to model. ...

Linq error generic parameter or the query must use a nullable type

I got this error when i use sum function in linq. The cast to value type 'Decimal' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type. GroupProduct.Where(a => a.Product.ProductID==1).Sum(Content => Content.Amount==null?0:Content.Amount), ...

SubSonic3 linq problem

I have this query: var rights = from gu in db.GroupUsers join g in db.Groups on gu.GroupId equals g.GroupId join gr in db.GroupRights on g.GroupId equals gr.GroupId join r in db.Rights on gr.RightId equals r.RightId where gu.UserId == userId && g.IsDeleted == false select ...

Stop linq from caching tables

Hi, when a new entity in my database is created and then i request a table from the datacontext the new entity does not appear. is linq caching the table and returning the cached version? if so how do i stop linq from doing this. DALConnector.Dc.Order.InsertOnSubmit(NewOrder); DALConnector.Dc.SubmitChanges(); now i click a button that...

Search XML doc with LINQ

Hi Guys/gals I have an xml doc similar to this: <Root> <MainItem ID="1"> <SubItem></SubItem> <SubItem></SubItem> <SubItem></SubItem> </MainItem> <MainItem ID="2"> <SubItem></SubItem> <SubItem></SubItem> <SubItem></SubItem> </MainItem> ... </Root> I want to return t...

OrderBy and Top in LINQ with good performance

What is a good way to get the top 10 records from a very large collection and use a custom OrderBy? If I use the LINQ to Objects OrderBy method it is slow and takes a lot of memory because it creates an entire new collection with the new order. I would like a new method with the signature below that does not re-order the entire collect...

Linq with group by having count

Hi, how do I write this query in linq (vb.net)? select B.Name from Company B group by B.Name having COUNT(1) > 1 ...

Does Enumerable.ToDictionary only retrieve what it needs?

I'm using Enumerable.ToDictionary to create a Dictionary off of a linq call: return (from term in dataContext.Terms where term.Name.StartsWith(text) select term).ToDictionary(t => t.TermID, t => t.Name); Will that call fetch the entirety of each term, or will it only retrieve the TermID and the Name fields from my data...

List<> OrderBy IComparer ?

Hi guys, I have a List<ListViewItem> and I've used it in a ListView in VirtualMode There are 5000 items in the list and I'm gonna sort it with LINQ and OrderBy whenever a Column of the ListView is clicked , so I have written below code : List<ListViewItem> ListOfItems = new List<ListViewItem>(); ListViewColumnSorter lvwColumnSorter; ...

How do i convert this linq code to inline sql

How would I covert this query to inline sql or a stored procedure? var a = from arow in context.post where arow.post_id == id && arow.post_isdeleted == false select new { arow.post_id, PostComments = from c in context.comment where c.CommentPostID == arow.post_id select new ...