linq

Cast LINQ result to ObservableCollection

The fact that it is a LINQ result might perhaps not be relevant for the question, but I'm mentioning it anyway - since this is the context which has resulted in this question. I run a LINQ query. The result is an; IEnumerable<MyClass> I want to put the result into an ObservableCollection; ObservableCollection<MyClass> How do I ...

System.LINQ.Dynamic: Select(" new (...)") into a List<T> (or any other enumerable collection of <T>)

Hi All, Say I have a DataTable with four columns, Company (string), Fund (string), State (string), Value(double): table1.Rows.Add("Company 1","Fund 1","NY",100)); table1.Rows.Add("Company 2","Fund 1","CA",200)); table1.Rows.Add("Company 3","Fund 1","FL",300)); table1.Rows.Add("Company 4","Fund 2","CA",400)); table1....

Returning a collection where the collection contains any of the first objects properties

I'm having a logic problem trying to return a collection of objects from a collection where the PersonId is contained in the personId of the first object. This sounds hideously complicated, but here it is: //classes public class MyClass { public List<int> People { get; set; } } //Code List<MyClass> myClasses = new List<MyClass>() { ...

How do I trace a linq query when using PredicateBuilder/AsExpandable?

I am using PredicateBuilder, which means that I am using the AsExpandable extension method. The problem is that I can no longer Trace my SQL queries as the following error is thrown when I try to cast the query to ObjectQuery so that I can do a ObjectQuery.ToTraceString() call on it... Unable to cast object of type 'LinqKit.ExpandableQu...

LINQ identity function?

Just a little niggle about LINQ syntax. I'm flattening an IEnumerable<IEnumerable<T>> with SelectMany(x => x). My problem is with the lambda expression x => x. It looks a bit ugly. Is there some static 'identity function' object that I can use instead of x => x? Something like SelectMany(IdentityFunction)? ...

In C# adding SelectMany extends linq to a new monad type, how do I do the same thing in VB.net?

An old Yet Another Language Geek blog post explaining monads describes adding a SelectMany extension method to C# in order to extend the linq syntax to new types. I've tried it in C# and it works. I did a straight conversion to VB.net and it doesn't work. Does anyone know if VB.net supports this feature or how to use it? Here is the C#...

Clone (deep copy) Entity LINQ

Hi, I would like to deep copy an Entity and I am looking for the best way to do it. I am also concerned about performances. I plan to have all my entities implementing ICloneable where Clone() will basically shadow copy and Clone all references. For instance: [DataContract()] class MyEntity { public int id; public strin...

Why does "linq to sql classes" change the name of a table when making a class?

I go in and add a new "linq to sql classes" in Visual Studio and then go and drag a table from Database Explorer to the new DBML and the name of the new class is no longer plural. What if I still want it to be plural? If I drag a table that isn't plural I get a bunch of compile errors about how there is already a type definition for al...

Filling in missing dates using a linq group by date query

I have a Linq query that basically counts how many entries were created on a particular day, which is done by grouping by year, month, day. The problem is that because some days won't have any entries I need to back fill those missing "calendar days" with an entry of 0 count. My guess is that this can probably be done with a Union or so...

Leaky abstraction in LINQ to SQL EntitySet

Hi, I’m having trouble with some dbml generated classes that don’t to resolve down to efficient SQL. Imagine I have an Accounts table and a Transactions table where each transaction is associated with a particular account. I load all this into dbml and out pops an Account class and a Transaction class. The Account class has an Entity...

LINQ to MySQL - what is the best option?

Has anyone used any of the utilities out there for LINQ to MySQL? Do you know which one is best? So far I know of LINQ for NHibernate, and DBLinq ...

Has Microsoft Fixed Linq to Entities Performance Problems in .NET 4.0?

I feel pretty comfortable with the entity framework now. I have been reading some performance articles on the Linq to Entities performance issues. Are these issues still present in .NET 4.0? ...

Using Linq find first object in list sorting by property A, then property B

I have an unordered list of Points (List<Point>) and I want to find the first Point in the list when ordering by X and then Y. NOTE: I don't want to actually change the order of the items in the List. ...

Linq to Sql - Repository Pattern - Dynamic OrderBy

Ok, I found this, which will allow me to do this: public IList<Item> GetItems(string orderbyColumn) { return _repository.GetItems().OrderBy(orderByColumn).ToList(); } Is this the best way to do "dynamic" ordering? I want to be able to pass the column name as a string (and the sort direction) to my Service, and have it order the c...

LINQ To SQL - Poor Sql Performance

Hey all, I have a WPF at a customer site that makes calls to a remote SQL 2005 server using Linq To Sql. Almost everyday the customer experiences aweful slow downs, and I am not sure what to do. The quick fix is to restart the mssql service and that seems to do the job, but that is not a solution. Tonight I used the SQL profiler and ...

LINQ: Doing an order by!

Hi there, i have some Linq to Entity code like so: var tablearows = Context.TableB.Include("TableA").Where(c => c.TableBID == 1).Select(c => c.TableA).ToList(); So i'm returning the results of TableA with TableB.TableBID = 1 That's all good Now how can I sort TableA by one of its column? There is a many to many relation ship betwee...

Compare two xml and print the difference using LINQ

I am comparing two xml and I have to print the difference. How can I achieve this using LINQ. I know I can use XML diff patch by Microsoft but I prefer to use LINQ . If you have any other idea I will implement that //First Xml <Books> <book> <id="20504" image="C01" name="C# in Depth"> </book> <book> <id="20505" image="C02...

User defined xml transformation

I have some sample xml data, for ex: <?xml version="1.0" encoding="utf-8"?> <Account> <User> <FirstName>John</FirstName> <LastName>Doe</LastName> </User> <LoginDetails Sequence="1"> <LoginDate>01/01/09</LoginDate> <Status>Successful</Status> </LoginDetails> <LoginDetails Sequence="2"> <LoginDate>01/02/09</LoginDate> <Status>...

Linq expression decimal culture

Hello, I have this piece of code: fee.SingleOrDefault(f => 100.05M >= f.Rate); In the database the Fee.Rate is a money field stored using invariant culture. But using this select gives an error because my current culture will convert "100.05" to "100,05" wihich results in; An expression of non-boolean type specified in a context whe...

Access columns in ItemDataBound event when the datasource is Linq

Im setting the the datasource with the following code: protected void Page_Load(object sender, EventArgs e) { var vacancies = from v in db.Vacancies join c in db.Customers on v.CustomerID equals c.CustomerID join cp in db.CustomerPortals on c.CustomerID equals cp.CustomerID ...