linq

Extract derived elements from a list of base elements

I have an interface list which stores a variety of objects derived from that interface. I would like to write a function where the caller specifies the type to extract. I've tried this: List<IParts> PartList; ... public List<IPart> Fetch(Type PartType) { return this.PartList.OfType<PartType>().Cast<IPart>().ToList(); } But it ...

Linq: using StringComparer with GroupBy/Distinct in query syntax

I have this (XLinq) query and was wondering how to convert it to the query syntax: var grouped = doc.Descendants() .GroupBy(t => t.Element(ns + "GroupingAttr").Value, StringComparer.OrdinalIgnoreCase); This is the query syntax without the StringComparer: var grouped = from t in doc.Descendants() group t...

"InnerText" on XmlNode in LINQ query do not work

When I call a property "InnerText" on XmlNode in LINQ query I am getting a strange error: "An item with the same key has already been added.". Query looks like: var partnersXml = from partnerTable in dataContext.SomeTableInDb where partnerTable.XmlType == "partner" select new { ...

LINQ: IEnumerable<KeyValuePair<SomeType, IEnumerable<OtherType>>> selection

How do I transform this IEnumerable<KeyValuePair<MyType, IEnumerable<OtherType>>> into new sequence of the same type, except I just want First() that is in the value (IEnumerable<OtherType>). Something like this (pseudo code): var toReturn = from kv in source let k = new KeyValuePair(kv.Key, kv.Value.First()) ...

EntitySet<IEnumerable<T>> to IEnumerable<T>

I have an EntitySet<IEnumerable<T>> returning from some query and need to cast it to IEnumerable<T>. Can I do it? ...

Retrieve single Entity Framework entities using a LINQ query or GetObjectKey?

It looks like GetObjectKey has the benefit of searching for existing, instantiated objects, and THEN the data store. However, it also seems like you lose some of the strong typing, and need to cast your resulting object: GetObjectKey int customerID = 1; EntityKey key = new EntityKey("MyEntities.Customers", "CustomerID", customerID); C...

Linq, repository pattern and database abstraction problem

Helo, i have spent some time now to solve a problem for which i have no solution till now. I have a predefined very huge database, whose structure is fixed. I use the repository pattern to create a abstraction layer between service code and database logic. The problem is, that i need to apply some processing to the database object befor...

Is my IQueryable syntax correct?

The generated SQL does a cross join but as the ID's are matched it acts like an inner join but is there a better performing way to do this by actually using the join keyword in C#? Is there a way where you don't have to specify how the properties join because they are all heirarchicly related anyway Item is a Page class PageRoles is an...

Searching for object in IList or IQueryable

Hi There I need to find a match of an item in a IQueryable list. I have a list as follows: IQueryable<EventItem> eventItems = new Queryable<EventItem>(); EventItem eventItem1 = new EventItem("Event 1"); EventItem eventItem2 = new EventItem("Event 2"); eventItems.Add(eventItem1); eventItems.Add(eventItem2); I now want to find the eve...

Linq - Using Except() on a Generic collection

I have asked this question about using the a Linq method that returns one object (First, Min, Max, etc) from of a generic collection. I now want to be able to use linq's Except() method and I am not sure how to do it. Perhaps the answer is just in front on me but think I need help. I have a generic method that fills in missing dates fo...

Reading vCalendar and vCard using .NET?

I want to be able to read vCard and vCalendar data using .NET, I have something which does this, and have looked at the specification. For those not familar with the format here is some test data from my current application: BEGIN:VCALENDAR VERSION:2.0 PRODID:-//Microsoft Corporation//Windows Calendar 1.0//EN CALSCALE:GREGORIAN METHOD...

How does IEnumerable<T>.Reverse work?

I am checking out the code in the reflector, but I haven't yet found out how it can enumerate through a collection backwards? Since there is no count information, and enumeration always starts from the "start" of the collection, right? Is it a drawback in the .NET framework? Is the cost higher than regular enumeration? ...

VB.NET/ASP.NET With and New keyword muddle

Can someone explain how New works with the With keyword in this example from the MVC framework. routes.MapRoute("Default", "{controller}/{action}/{id}", New With {.controller = "Home", .action = "Index", .id = ""}) ...

Whats the best SQL Query to get Related Items?

I have a small site where I want to get Related Videos on basis of Tags... what could be the best MS SQL 2005 query to get related Videos on basis of Tags. If you can give LINQ query that would be awsome. Here is Database Schema: CREATE TABLE Videos (VideoID bigint not null , Title varchar(100) NULL, Tags varchar(MAX) NUL...

Best SQL 2005 Query to get Related Items?

I have a small video site where I want to get related videos on the basis of the most matched tags. What would be the best MSSQL 2005 query to get the related videos? A LINQ query would be appreciated as well. Schema: CREATE TABLE Videos (VideoID bigint not null , Title varchar(100) NULL, isActive bit NULL ) CREATE T...

Return typed DataTable from Linq Query

Hi. I have a (disconnected) typed Dataset in an XML, which I query in LINQ: var productlist = from prds in dsProducts.Products.AsEnumerable() where (prds.Field<string>("productid").Contains(searchpattern) || prds.Field<string>("productname").Contains(searchpattern)) select prds; This list is fine, but if I try to say: return ...

How to get Max String Length in every Column of a Datatable

I have a DataTable object. Every column is of type string. Using LINQ, how can I get the maximum string length for every column? ...

BindableLinq collection nesting

I'm wondering if there is a way to do this in bindable linq: var LeftItems = AllItems.Where(p => !RightItems.Contains(p))); I have tried liberal use of the AsBindable(), but it doesn't work for me.. var LeftItems = AllItems.AsBindable().Where(p => !RightItems.AsBindable.Contains(p))); If this is not supported in BindableLINQ, is th...

I have a Tags Table. How to Bulk Insert using LINQ?

I am using VB.NET with LINQ to MS SQL. I have two following tables. Now I want to insert multiple items in Tags Table using LINQ, but also wants to check that if any of tag exists in Tags table. It doesn't insert it again and bring the TagID in both cases (if inserted or if found existed) CREATE TABLE Tags (TagID bigint not null , ...

LINQ to XML when a node does not exist

Hi there, I was writing a generic class to read RSS feed from various source and to consolidate in one collection of object in VB.net. Basically the function - using LINQ to XML - is working properly, but I have a problem when the RSS feed I am trying to read does not contain one of the node (as you know, many of them are optional). I w...