linq

Remove 3 oldest elements from a List<> in C#

Let's say I have an object: public class CustomObj { DateTime Date { get; set; } String Name { get; set; } } Then let's say I have a List with 20 various elements. var stuff = new List<CustomObj> { { Date = DateTime.Now, Name = "Joe" }, { Date = DateTime.Now.AddDays(1), Name = "Joe2" }, { Date = DateTime.Now.AddDa...

Linq to entities - searching in EntityCollection navigation properties.

We have classes public Invoice: EntityObject { public EntityCollection<InvoicePosition> Positions { get {...}; set{...}; } ... } public InvoicePosition: EntityObject { public string GroupName { get {...}; set{...}; } } We are given IQueryable<Invoice>, we are not given IQueryable<InvoicePosition>. How should I find invoic...

LINQ: DataContext Life and System.Data.SqlClient.SqlException: Timeout expired.

Hi there, i seem to be getting a lot of this in my Linq 2 SQL System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. There is really no reason for it, its a simple query that returns 1 record. I was thinking about opening my datacontext w...

NullReferenceException on trying to enumerate an empty IQueryable.

I am getting a NullReferenceException when I try and convert the following LINQ to EF query. What could be wrong here? List<DailyProductionRecord> prodRecs = new List<DailyProductionRecord>(); var recs = from p in productionEntities.DailyProductionRecordSet where ((p.Department.DeptId == d...

How to generate BlToolkit model

From what I have seen on the ormbattle.net, BlToolkit is the fastest ORM for .NET. I would like to test it in one of my projects, but I don't know how to generate entity classes. Should I do this by hand? I have 20+ tables in the database. Is there any online tutorial or blog, where I can find more information about BlToolkit? It looks ...

Generating very large XML file with Linq-to-XML and Linq-to-SQL

I'm trying to do a dump to XML of a very large database (many gigabytes). I'm using Linq-to-SQL to get the data out of the database and Linq-to-XML to generate XML. I'm using XStreamingElement to keep memory use low. The job still allocates all available memory, however, before keeling over without having written any XML. The structure l...

LINQ to SQL classes to my own classes

I'm looking at using LINQ to SQL for a new project I'm working on, but I do not want to expose the LINQ classes to my applications. For example, do an select in link returns a System.Linq.IQueryable<> collection. As well, all the classes generated to represent the database use Table, Column, EntityRef classes and attributes. It's fine...

Create a list of one object type from a list of another using Linq

If I have classes of Type A and B: public class A { public int TotalCount; public string Title; } public class B { public int Count; public string Title; } I have a list of instances A instances, what is the most efficient way to create and populate a List of type B using Linq? ...

LINQ to SQL - custom function

I would like to run a LINQ query like this: var words = from p in db.Words where p.Document.Corpus.Name == corpus //where LevenshteinDistance(p.Text.ToCharArray(), word.ToCharArray()) < threshold select p; But if I place the "LevenshteinDistance" function in there it will generate an error: NotSupportedException: Method '...

Linq to SQL nvarchar problem

Hi, I have discovered a huge performance problem in Linq to SQL. When selecting from a table using strings, the parameters passed to sql server are always nvarchar, even when the sql table is a varchar. This results in table scans instead of seeks, a massive performance issue. var q = ( from a in tbl where a.index == "TEST" s...

how to bind the dropdown list

Please help I am getting the following error when I am binding the drop down list in the form Unable to cast object of type 'System.Data.Objects.ObjectQuery1[<>f__AnonymousType02[System.String,System.Int32]]' to type 'System.Collections.Generic.List`1 I have written the query in Linq language return (IList )(from p in db.tbl_PRODUCT_C...

LINQ-to-sql + C#. Only retrieve some columns from table to gridview

Hello. I try to pull some data and insert it into a gridview. But I must be doing something wrong since I can only select a single column. If I try to get both first and lastname then they will just be inserted into the same td in the gridview. Method so far is: public string[] ShowName() { LinqToEbuboo_20DataContext db = ...

LINQ : understanding and executing Groupby to bring back unique rows?

Hi there, i am trying to do a groupby in linq, basically i have a list ( along list - around 1000 entries) and i wish to groupby Description. The entries are vehicles, so hence there are 50 or so Ford Mondeos My query is pretty simple, no joins (yet :-) ) but it brings back a list including 50 Ford Mondeos, i wanted it to group them s...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an enumeration. I have some working code which allows me to group together lists of files by FileType. var fileGroups = fileList.GroupBy(f=> f.FileT...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index of GenId from my list that have TestMode == 0 if the same GenId has a TestMode == 1. does someone have a terse way of doing this? ...

LINQ: Help with linq query and contains for an IEnumerable<string>?

Hi there, Can anyone help? I have a linq query which is embedded inside a extension method, it was working as v.RentalStatus was a String. I am now using a Group on my original query (the query is quite complex so i won't put it here). The importante thing is that v.RentalStatus = IEnumerable hence it can contain things like A (meani...

LINQ : saving files to a database

I want to save a pdf and mp3 file(s) to a SQL Server database and be able to retrieve from it. I'm still starting out with LINQ and don't master it yet. ...

Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?

When using GTK# from C# on Mono, I often find myself copying out C# data structures into Gtk.ListStore -- it would be much easier if there was a Gtk.TreeModel which wrapped an IEnumerable. Does such a thing exist? ...

"Only arguments that can be evaluated on the client are supported for the String.Contains method"

public static void MyFunction(MyErrorClass err) { var query = from filter in DataContext.ErrorFilters select filter; query = query.Where(f => err.ErrorMessage.Contains(f.ErrorMessage)); List<ErrorFilter> filters = query.ToList(); //...more code } So I'm having some issues with the above code, and I'm getting the error from the ...

Multiple SUM using LINQ

I have a loop like the following, can I do the same using multiple SUM? foreach (var detail in ArticleLedgerEntries.Where(pd => pd.LedgerEntryType == LedgerEntryTypeTypes.Unload && pd.InventoryType == InventoryTypes.Finished)) { weight += detail.GrossWeight; lengt...