linq

Different DataContext error. Why is this a problem?

The following code is causing a "This query contains references to items defined on a different data context" error. My 2 data contexts are created with 2 nested using blocks around the code that calls this method and displays its results on screen. The methods that this method calls only use the data context passed in to them, they don'...

C#: Is there a LINQ way to create an array of objects given an array of constructor parameters?

As an example, say I have an array of names and I want to create an array of Person objects by calling a constructor that takes string name. class Person() { public string Name { get; set; } public Person(string name) { Name = name; } } ... static void Main() { string[] names = {"Peter", "Paul", "Mary"}; ...

Why is the LINQ "apply-to-all" method named Select?

When I read code that uses Select I think "select-all-where". When I read code that uses Map I think "this-to-that" or "apply-to-all". I can't be the only person that feels the name Select is confusing. Map ...

find inside array of objects

i have a objectA public class objectA { public int Id; public string Name; } i have a list of objectA List<objectA> list; i want to find in the list any objectA with Id = 10; is there linq syntax for this or do i simply have to write a loop here. ...

Linq to Nhibernate Bulk Update Query Equivelent?

Hi, Not sure if I'm missing anything here. Basically, I am looking for Linq to Nhibernate to do the following SQL statement: update SomeTable set SomeInteger = (SomeInteger + 1) where SomeInteger > @NotSoMagicNumber Is there any way to do that? Thanks! ...

linq where condition on subselect

how can we add a where condition to a linq subselect query. i.e. List<CallLog> callLog = CallLog.SampleData(); List<Contacts> contacts = Contacts.SampleData(); var q = from call in callLog where call.Incoming == true group call by call.Number into g select new contacts { contact....

Convert this SQL query to Linq (Not Exists + sub query)

I would like this SQL to be converted to LINQ. (it shouldl select rows from input which do not exist in table production based on 3 columns. If a column in both tables contains NULL, it should be considered as having the same value) SELECT i.* FROM INPUT AS i WHERE NOT EXISTS (SELECT p.Agent FROM Production AS p WHERE ISNULL(i.CustID,''...

LINQ? Refactoring foreach...

Is there a better way to write this? I feel like I'm getting rusty with C# after doing a lot of JavaScript lately. Can this be improved? foreach (var item in this.CartItems) { if (item.EffectivePrice != null) { this.CartItems[this.CartItems.IndexOf(item)].EffectivePrice = CurrencyHelp...

Keeping Duplicates

I have an IEnumerable containing objects that have a groupnumber property. I want to be able to get a list of all objects that have duplicate groupnumbers e.g. obj1: groupnumber=1 KEEP obj2: groupnumber=2 DELETE obj3: groupnumber=1 KEEP I can use the following to get a list of all the duplicated groupnumbers var duplicates = from...

"No supported translation in SQL" Linq workaround?

Currently im trying to make my query short with reusable peice of code like this to check for post if it's eligible to display. // Logic to check if post is eligible for display public bool isEligibleForDisplay(Post n) { var pubDate = n.PUBLISH_DATE ?? DateTime.MinValue; var endDate = n.END_DATE ?? DateTime.M...

Convert linq method syntax to query syntax

Not that it would be better, but I'm trying to get my head around turning the following method syntax to query syntax to see the difference. long diskSpace = Directory.EnumerateDirectories(@"c:\") .SelectMany(Directory.EnumerateFiles) .Sum(fileSize => new FileInfo(fileSize).Length); ...

Searching Hierarchical List

I've got a simple class defined as: public class IndexEntry { public bool HighScore { get; set; } public List<IndexEntry> SubEntries { get; set; } //Other properties, etc... } I now need to search through a List to find the one item that has its HighScore Property set to true. Since it isn't a flat list, but a Hierarchy whic...

Linq Query not returning IEnumerable.

I have the follow Linq query that is in a web application that was converted from .NET 1.1 to 3.5: dim objListOfFilteredDataRows = from datarows as datarow in objDataSet.tables(0).rows _ where datarows("SomeColumn") = SomeValue I have the exact same query in an application that was created using .NET 3....

LINQ method for adding items to a dictionary

I'm trying to learn a bit more about LINQ by implementing Peter Norvig's spelling corrector in C#. The first part involves taking a large file of words (about 1 million) and putting it into a dictionary where the key is the word and the value is the number of occurrences. I'd normally do this like so: foreach (var word in allWords) ...

Switching Database in LinqToSql

How can we change the underlying database for Linq based WebApp ? for example: When we release our web application, say to release from production, if using ADO.NET, it is as simple as modifying connection string in web.config to point towards the live Database in use. The databases are almost identical, other data stored.. What and h...

Binding linq result to list box

Hi , i have the following piece of code, that for some reason that i'm unaware of, doesn't populate the LINQ resultset to the listbox (and there are many results in this list), however, i bind it to the original datatable, it works well. any ideas: DataTable t = _partitionsDataSet.Tables[0]; var customizedPartitions = ...

Why is dbml generating extra classes

i am having a small problem here. The table which dbml (LinqToSql designer) is using has columns (Foreign keys), for which it generates two objectsfor the coressponding Blogs table (1:1 association) in DB lmost same, like Table Authors: AuthorID INT, Name varchar(20), BlogID INT And TABLE BLOG: BlogID INT, Name varchar(MAX) Bl...

How can I combine these linq queries into one?

Being new to LINQ, I created a couple queries and would like to combine them into one, but I am not sure how to do it. Here they are: var u = dc.Users.Where(w => w.UserName == userName).SingleOrDefault(); var m = dc.Memberships.Where(w => w.UserId == u.UserId).SingleOrDefault(); m.PasswordQuestion = securityQuestion; m.PasswordAnswer...

How to implement tag counting

I have my tags desinged like this in my database: Table: Item Columns: ItemID, Title, Content Table: Tag Columns: TagID, Title Table: ItemTag Columns: ItemID, TagID //example -- this is the right sidebar of stackoverflow c# × 59279 sql × 14885 asp.net-mvc × 9123 linq × 4337 tags × 339 if I wanted to know the count of each ta...

Random linq result

Hello, I have this LINQ query: var agnts = (from a in db.agents select new { a.UserId, a.name }).Take(10); How can I get randomly get 10 records from my agents table? I have tried: agnts = agnts.OrderBy(n => Guid.NewGuid()); but this doesn't seem to do anything. I would appreciate anybody's help on this. Thanks, Louis ...