linq

What is the max LINQ Expression Trees can do?

What is the maximun that LINQ expression Tree can do? Can it define a class? How about a method, with all the declared name, modifiers, parametertype and return type? Must the program always define the tree himself? Is it possible to generate the tree from a given C# file? Where can I get resources to learn about writing basic to adv...

Anyone found a use of "var" other than for LINQ?

Just curious. I'm about 99.999% sure there is none...but anything? EDIT: These are OK answers (saving typing time or making the code less verbose for "readability"). I guess I should have clarified what I meant by "use" - some construct/design that couldn't be done without "var" . ...

Unable to cast object

I am new to LINQ, I tried to run the following code and I got InvalidCastException error: "Unable to cast object of type 'd__3a`1[debug.Product]' to type 'debug.Product'" - what is wrong? Code (VB - using VS2008) Private Sub btnLinq_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLinq.Click Dim ...

How to build this complex Linq expression ?

Hi! I want to build a System.Linq.Expression from the string List like this: System.Linq.Expressions.Expression x = null; foreach (string s in GetWords(input)) { /* Create Expression */ } so I could use: .Where(x => x.Name.Like(string.Format("%{0}%", word1)) || x.Name.Like(string.Format("%{0}%", word2)) || ...

Has anyone every received a "Unhandled Expression Type: 1004" using Linq to NHibernate?

I have a linq query that is passed into NHibernate which Linq2NHibernate will parse and return me populated entities. string firstName = "Ryan"; Store store = _repository.Query<Store>().Where(x => x.Employees.Select(y => y.FirstName).Contains(firstName)).FirstOrDefault(); The troublesome part is x => x.Employees.Select(y => y.FirstNam...

LINQ Aggregate vs. nested foreach

I am trying to achieve: foreach (ScheduleItem s in ScheduleItems) { foreach (IScheduleModule m in s.ScheduleModules) { yield return m; } } using LINQ aggregate and I do not understand why return ScheduleItems.Aggregate(new Collection<IScheduleModule>(), (x, o) => x.Union(o.ScheduleModules) as Collection<IScheduleM...

ASP.NET MVC - Model.OrderBy Date has no effect

Hello, I'm having some difficulties to sort my results by Date. Is there any special method? Because I'm doing this right now: var db = new DB(); var articles = db.Articles; var orderedArticles = articles.OrderBy(a => a.Date); return View(orderedArticles.ToList()); Where Date is a datetime field. And there is no effect for OrderBy(..)...

dynamic linq contains

Hello all, i search for a dynamic linq-to-sql Contains (StartsWith / EndsWith) method. I’ve tried the following code, but it didn't work. Any ideas? public static IQueryable<T> WhereContains<T, V>(this IQueryable<T> queryable, string propertyName, V propertyValue) { ParameterExpression pe = Expression.Parameter(typeof(T), "p"); ...

Linq-to-SQL forces me to Rebuild to see changes. Why?

In one of my current projects we keep running into this very wierd problem with Linq-to-Sql: If we update some properties of some objects (that are mapped to the db) and then SubmitChanges as usual, we are not able to see these changes until we rebuild the entire solution in visual studio. How come? ...

Problem with Json

Im having this weird problem with creating Json dynamically.... for some reason this doesnt work var jsonData = new { total = totalPages, page = page, records = totalRecords, rows = ( from company in companies select new { ...

Perform multiple LINQ updates to avoid uniquekey exception

Hi, I have a database with a Unique key on the the columns ParentRef and SortIndex. In LINQ, I want to switch two SortIndex values. I want to do this in one transaction so there can be multiple users at once. How do I, with LINQ, switch the values in one go, so my Unique key does not be violated? var dc = new MyDataContext(); ...

Hierarchy Problem -> Replace Recursion with Linq Join??

I have a self referential table, which has ID, ParentID (nullable). So, the table contains many nodes, each node could be the root in the hierarchy (parent is null), or any level of the hierarchy (parent exists elsewhere in the table). Given an arbitrary starting node, is there an elegant linq query that will return all children of the...

Linq to SQL: Multi-table join return type not generated by dbml

Hello, I'm trying to figure out the best way to handle a simple problem: I have a simple LINQ join to two tables. I know how to return the type for one table, since it is the same as the generated dbml class. However, what if I want to return data from both tables- isn't there a way to return both and use their relationships? Do I rea...

Error Binding Gridview: "The current TransactionScope is already complete"

I am doing cascading deletes in an event sent from a Gridview. The deletes are in a Transaction. Here is the simplified code: protected void btnDeleteUser_Click(object sender, EventArgs e) { DataContext db; db = new DataContext(); using (TransactionScope ts = new TransactionScope()) { try { /...

How to generate private Linq to SQL classes?

I'm trying to figure out how to make the generated Linq to SQL classes be marked private instead of public, so that it is not visible outside the assembly. There doesn't seem to be a way to do this in the Visual Studio O/R designer or the SqlMetal tool, unless I'm missing something. Has anyone found a way to do this? I'm asking becaus...

C# - Select XML Descendants with Linq

I have the following XML structure: <row> <field name="Id">1</field> <field name="AreaId">1</field> <field name="Name">ת&quot;א</field> </row> <row> <field name="Id">2</field> <field name="AreaId">4</field> <field name="Name">אבטליון</field> </row> I want to iterate over the name nodes with Linq. I tried this: var items =...

LINQ and Null Coallescing

I'm trying to get the null coallescing operator to work with a LINQ expression in ASP.NET MVC. Here is my expression. <%= Html.Encode(item.Ring.Keys.Single<ProjectOne.Key>( k => k.Literal.Value == "Class" ).KeyValue) %> Basically, it finds an instance of the "Key" class based on the given Name, the name being the title, the result be...

LINQ to SQL read error - how to determine which column failed?

I have a LINQ to SQL class that I am reading into a List. When reading in from the table, I get an error "String must be exactly 1 character long". I have looked over the Table definition for this file, and the .dbml file for this table, and there is no field that is exactly 1 char long. The table (a legacy table) has a lot of fields...

How Does Queryable.OfType Work?

Important The question is not "What does Queryable.OfType do, it's "how does the code I see there accomplish that?" Reflecting on Queryable.OfType, I see (after some cleanup): public static IQueryable<TResult> OfType<TResult>(this IQueryable source) { return (IQueryable<TResult>)source.Provider.CreateQuery( ...

SubSonic3 Linq query generates "IS NOT NULL" instead of "IS NULL"

here is my linq query: var test = from m in db.Members where m.UserId == null select m.Id; test.ToList(); UserId is a nullable Guid field on the members table that corresponds to the ASP.NET membership table aspnet_member. I am unable to generate a query through subsonic that will select where the userid IS null, only where ...