linq-to-objects

Case insensitive string compare with linq-to-sql and linq-to-objects

妖精的绣舞 提交于 2019-12-01 16:04:20
see also Differences between LINQ to Objects and LINQ to SQL queries We are using the some queries over our database and our in memory objects . What is the best way of doing an insensitive string compare with linq-to-sql so that? It runs fast on SQL Server The same query expression can be used with linq-to-objects to get the same result Using a.ToLowerInvariant() == b.ToLowerInvariant() at least gets the same results, but it does not get processed on SQL server as far as I can tell, so can be a lot slower then a == b I'm not an expert in linq to sql, but you could just use the

SortedSet / SortedList with better LINQ performance?

ε祈祈猫儿з 提交于 2019-12-01 15:53:24
问题 Let's say we have a sorted collection such as SortedSet or SortedList with many (10M+) elements. Lots of querying is happening, so performance matters. From runtime comparisons, I'm under the impression that LINQ to Objects doesn't take advantage of the sorting, therefore not taking advantage of potential performance gains. First example - counting the elements in a range: var mySortedSet1 = new SortedSet<int>(); // populate ... int rangeCount = (from n in mySortedSet1 where ((n >= 1000000000

How to implement SkipWhile with Linq to Sql without first loading the whole list into memory?

与世无争的帅哥 提交于 2019-12-01 15:22:07
I need to order the articles stored in a database by descending publication date and then take the first 20 records after the article with Id == 100 . This is what I would like to do with Linq: IQueryable<Article> articles = db.Articles .OrderByDescending(a => a.PublicationDate) .SkipWhile(a => a.Id != 100) .Take(20); However, this generates a NotSupportedException because SkipWhile is not supported in Linq to Sql (see here ). A possible solution is to execute the query and then apply SkipWhile using Linq to Object: IEnumerable<ArticleDescriptor> articles = db.Articles .OrderByDescending(a =>

How to implement SkipWhile with Linq to Sql without first loading the whole list into memory?

孤街浪徒 提交于 2019-12-01 14:16:26
问题 I need to order the articles stored in a database by descending publication date and then take the first 20 records after the article with Id == 100 . This is what I would like to do with Linq: IQueryable<Article> articles = db.Articles .OrderByDescending(a => a.PublicationDate) .SkipWhile(a => a.Id != 100) .Take(20); However, this generates a NotSupportedException because SkipWhile is not supported in Linq to Sql (see here). A possible solution is to execute the query and then apply

How linq 2 sql is translated to TSQL

[亡魂溺海] 提交于 2019-12-01 13:59:31
It seems Linq2sql doesn't know how to construct the TSQL while you transform linq2sql object into domain object with constructors. Such as: from c in db.Companies select new Company (c.ID, c.Name, c.Location).Where(x => x.Name =="Roy"); But when using settable attributes, it will be OK. from c in db.Companies select new Company { ID = c.ID, Name = c.Name, Location = c.Location }.Where(x => x.Name =="Roy"); I don't want to allow those attributes to be settable. How can I achieve this? And can anybody provide food for thought on how linq 2 sql is translated into TSQL? Thanks in advance! It's

Filtering on template list with property name as string

岁酱吖の 提交于 2019-12-01 11:22:16
问题 Hi I have to apply filter on generic class. The sample class is as follows public class Sample<T> { List<T> sourceList = new List<T>(); public void applyFilter(string propertyName , EnumOperator operator , object value) { } } Here I want to implement filter using linq or dynamic linq but am not getting any positive direction to implement this functionality. Please give me some positive direction so that I can implement this functionality. Thanks. 回答1: I would recommend returning an filtered

Can I use Expression<Func<T, bool>> and reliably see which properties are referenced in the Func<T, bool>?

折月煮酒 提交于 2019-12-01 10:47:22
问题 I'm writing something in the flavour of Enumerable.Where in that takes a predicate of the form Func<T, bool> . If the underlying T implements INotifyPropertyChanged , I'd like to be a bit more intelligent about re-evaluating the predicate. I'm thinking about changing it to use Expression<Func<T, bool>> , and then using the expression tree to find out which properties are used in the predicate. Then I can have my PropertyChanged handler be a bit more intelligent. My question: is this feasible?

Linq dynamic queries for user search screens

扶醉桌前 提交于 2019-12-01 09:37:22
I have a database that has a user search screen that is "dynamic" in that I can add additional search criteria on the fly based on what columns are available in the particular view the search is based on and it will allow the user to use them immediately. Previously I had been using nettiers for this database, but now I am programming a new application against it using RIA and EntFramework 4 and LINQ. I currently have 2 tables that are used for this, one that fills the combobox with the available search string patterns: LastName LastName, FirstName Phone etc.... then I have an other table that

Call function in dynamic linq

亡梦爱人 提交于 2019-11-30 14:38:58
I'm trying to call a function in a dynamic linq select statement, but im getting error: No property or field 'A' exists in type 'Tuple2' Example code: void Main() { var a = new Tuple<int, int>(1,1); var b = new[]{ a }; var q = b.AsQueryable().Select("A.Test(it.Item1)"); q.Dump(); } public static class A { public static int Test(int i) { return i++; } } How should I change my code to get this working? If I call built in function Convert.ToInt32 for example it works fine. var q = b.AsQueryable().Select("Convert.ToInt32(it.Item1)"); Also how do I cast a property using dynamic linq? var q = b

LINQ GroupBy continuous time

对着背影说爱祢 提交于 2019-11-30 12:47:42
Assuming I have a simple structure that looks like this: public class Range { public DateTime Start { get; set; } public DateTime End { get; set; } public Range(DateTime start, DateTime end) { this.Start = start; this.End = end; } } And I create an collection like so: var dr1 = new Range(new DateTime(2011, 11, 1, 12, 0, 0), new DateTime(2011, 11, 1, 13, 0, 0)); var dr2 = new Range(new DateTime(2011, 11, 1, 13, 0, 0), new DateTime(2011, 11, 1, 14, 0, 0)); var dr3 = new Range(new DateTime(2011, 11, 1, 14, 0, 0), new DateTime(2011, 11, 1, 15, 0, 0)); var dr4 = new Range(new DateTime(2011, 11, 1,