linq

LINQ-to-objects index within a group + for different groupings (aka ROW_NUMBER with PARTITION BY equivalent)

After much Google searching and code experimentation, I'm stumped on a complex C# LINQ-to-objects problem which in SQL would be easy to solve with a pair of ROW_NUMBER()...PARTITION BY functions and a subquery or two. Here's, in words, what I'm trying to do in code-- the underlying requirement is removing duplicate documents from a lis...

How to get distinct instance from a list by Lamba or LINQ

I have a class like this: class MyClass<T> { public string value1 { get; set; } public T objT { get; set; } } and a list of this class. I would like to use .net 3.5 lambda or linq to get a list of MyClass by distinct value1. I guess this is possible and much simpler than the way in .net 2.0 to cache a list like this: List<MyC...

Prevent nullable parameter in stored procedure with LINQ

Hi, I want to define stored procedure parameters which do not allow null arguments. That @MailItemId int OUTPUT will be not nullable because when I import the stored procedure into the LINQ to SQL designer it says @MailIetmid is ref int? mailItemId. Thanks. ...

Entity Framework Metadata LINQ (Converting from C# to VB.NET)

I've got a query in C# that is working for me to query the metadata for the Entity Framework. I need to convert it to VB.NET, but I'm struggling to convert the AS keyword to "cast" meta to System.Data.Metadata.Edm.EntityType. I've tried TryCast, CType, Cast, etc. Here's the query in C#: var queryResult = from meta in oc.MetadataWorks...

Linq to xml error

Hello, i have a problem with my linq to xml query var q = (from f in xmlLang.Element("lang").Elements("page") where (string)f.Attribute("id") == "home" select f.Element(LangElement).Value.ToString()).Take(1).SingleOrDefault(); The xml looks like this, <lang> <page id="home"> <hello>Hello!</hello>...

How to compute a running sum of a series of ints in a Linq query?

I am trying to come up with a linq query to convert an IEnumerable<int> to another IEnumerable<int>, where each int in the result is the sum of all the ints up to that position from the initial list: Given int[] a I need int[] b Where b[0] = a[0], b[1] = a[0] + a[1], b[2] = a[0] + a[1] + a[2] and so on Alternatively, the sums above can...

LINQ: From a list of type T, retrieve only objects of a certain subclass S

Given a simple inheritance hierarchy: Person -> Student, Teacher, Staff Say I have a list of Persons, L. In that list are some Students, Teachers, and Staff. Using LINQ and C#, is there a way I could write a method that could retrieve only a particular type of person? I know I can do something like: var peopleIWant = L.OfType< Teache...

Do LINQ queries have a lot of overhead?

Are simple LINQ queries on an IEnumerable<T> lightweight or heavyweight? How do they compare to writing for or foreach loops by hand? Is there a general guideline for when to prefer LINQ versus manual searching? For example: var lowNums = from n in numbers where n < 5 select n; Compared to: List<int> lowNums = new List<i...

Remove instances from a list by using LINQ or Lambda?

Now I come a stage to get all my data as a list in cache(objects) and my next thing I have to do is to remove some instances from the list. Normally, I would do removing like this: List<T> list; List<T2> toBeRemovedItems; // populate two lists foreach(T2 item in toBeRemovedItems) { list.Remove(delegate(T one) { // build a condit...

LINQ help for Dynamic Data Website

Hi All, I have Dynamic dataWebsite which uses a SQL SP to do update operations..I have a problem here, my delete functionality is also a update(setting IsDeleted=1) operations. I am currently using LINQ query and calling datacontext.SubmitChanges() for deleting. The problem is, the update LINQ query(that sets IsDeleted=1) when i call Su...

Get list of tables (objects) from Entity framework model (edmx)?

How to get list of tables (objects) from Entity framework model (edmx)? In LinqToSQL model, it's easy context.Mapping.GetTables(), but how in EF.. ...

expression trees linq get value of a parameter ?

AddOptional<tblObject>(x =>x.Title, objectToSend.SupplementaryData); private static void AddOptional<TType>(Expression<Func<TType,string>> expr, Dictionary<string, string> dictionary) { string propertyName; string propertyValue; Expression expression = (Expression)expr; while (expression.NodeType == ExpressionType.Lambd...

how do i get the min from a linq to dataset query

Hi i have a linq query below var Free = (from row in dt.AsEnumerable() where row.Field("AppointmentType") == "FreeTime" select new{ row.Field("BookedDate") row.Field("TravelTime")}).Min() what i want to do is have a minimum on the travelT...

Linq Query on int using string

I'm trying to query the unique reference number of a table using Linq to Entities. The ID is provided via a textbox and hence a string in my code. Obviously in the database table the field is an integer so I have tried using .ToString first and then .Contains in the same way you would with a varchar(). This doesn't seem to work, with the...

Update Data Model Business Entity

hello, I have web application where Iam using linq to business entites i have business data model. the problem is : I have table with one column that it dosen't allow null value, when I try to update this table the folloeing error arise: error The property 'e.g Carrier' is part of the object's key information and cannot be modified w...

Linq to Sql - Set connection string dynamically based on environment variable

I need to set my connection string for Linq to Sql based on an environment variable. I have a function which will return the connection string from the web.config based on the environment variable, but how do I get Linq to always use this "dynamically created" connection string (preferably without having to specify it every time)? I kn...

Linq Query Using DataTable with Paging

I have a Linq query that I copy to a DataTable which is then used to populate a gridview. I am using a group by "key" and "count" which I evaluate in the aspx page for a master/detail gridview with a repeater. The problem that I am encountering is that the gridview datasource and bind to the datatable is not presenting me with any addi...

Need help with a linq query please!

I am trying to write a query that will search for "orders" that contain a certain "product" and I'm having a little difficulty getting it to work. Basically, this is what I'm trying to do: Dim orders = From o in db.Orders _ Where o.OrderProducts.Contains(Function(p) p.ProductID = 123) _ Select o I've also tr...

LINQ to SQL: Is it possible to reference the data context when extending a table object?

If I'm extending OnCreated for a LINQ to SQL table object, is it possible to get a reference to the data context to which the table belongs? For example, if I add a property to the data context: Partial Class MyDataContext Private _myValue As String Public ReadOnly Property MyValue As String Get Return _myValu...

Preserve order of values with linq and ToLookup()

While I'm pretty certain that the keys are unordered when returned from linq's ToLookup method, Is the order of values preserved? I can't find any documentation that says one way or the other. ...