linq

LinQ to Entities: Lambda expressions

Hi! I want to translate this var db = new PracticeEntities(); var destinations = db.DestinationDetails. Where(dd => dd.Language.Lang == "en-US" ...

How do I pass a Linq query to a method?

I'd like to pass a Linq query to a method, how do I specify the argument type? My link query look something like: var query = from p in pointList where p.X < 100 select new {X = p.X, Y = p.Y} clearly I'm new to Linq, and will probably get rid of the receiving method eventually when I convert the rest of my code, but it se...

LINQ Except operator and object equality

Here is an interesting issue I noticed when using the Except Operator: I have list of users from which I want to exclude some users: The list of users is coming from an XML file: The code goes like this: interface IUser { int ID { get; set; } string Name { get; set; } } class Use...

Is it bad to call First() multiple times in a LINQ Select?

I have a LINQ statement where I'd like to merge in the First address with the Nickname of 'Me'. using (var ctx = new DataEntities()) { return from c in ctx.Customers.Include("Addresses") let m = from a in c.Addresses where a.Nickname == "Me" select a where m.Any() select new { Id = c.Cust...

linq aggregate

class Category { public string Name { get; set; } public int Count { get; set;} } Name Count AA 2 BB 3 AA 4 I have an IEnumerable<Category> and would like to get a list of Categories with unique names and the sum of multiple entries Output Name Count AA 6 BB 3 Update class Category { public string Na...

How to convert a list of ints to a string array in Linq?

Hi folks, i have the an IList and i wish to turn it into a ToArray() string result. Currently i have to do the following :( List<string> values = new List<string>(); foreach(var value in numberList) { values.Add(value.ToString()); } ... string blah = string.Join(",", values.ToArray()); i was hoping to remove the foreach and...

OrderBy Signature Error in Pro LINQ book?

According to Pro LINQ: Language Integrated Query in C# 2008, Prototype of OrderBy operator is public static IOrderedEnumerable<T> OrderBy<T, K>( this IEnumerable<T> source, Func<T, K> keySelector) where K : IComparable<K> But the MSDN documentation does not have a generics contraint on TKey that it should be of type IComp...

LINQ - Left Join, Group By, and Count

Let's say I have this SQL: SELECT p.ParentId, COUNT(c.ChildId) FROM ParentTable p LEFT OUTER JOIN ChildTable c ON p.ParentId = c.ChildParentId GROUP BY p.ParentId How can I translate this into LINQ to SQL? I got stuck at the COUNT(c.ChildId), the generated SQL always seems to output COUNT(*). Here's what I got so far: from p in con...

Performing part of a IQueryable query and deferring the rest to Linq for Objects

I have a Linq provider that sucessfully goes and gets data from my chosen datasource, but what I would like to do now that I have my filtered resultset, is allow Linq to Objects to process the rest of the Expression tree (for things like Joins, projection etc) My thought was that I could just replace the expression constant that contain...

LINQ queries on possibly infinite lists

I am currently doing some Project Euler problems and the earlier ones often involve things like Fibonacci numbers or primes. Iterating over them seems to be a natural fit for LINQ, at least in readability and perceived "elegance" of the code (I'm trying to use language-specific features where possible and applicable to get a feel for the...

Update in asp.net with linq not working

hi everyone I have a strange problem. I am trying to update some fields using linq and it is not working. I tried to build the site and debug it which works and shows that the function is called. I also tried manually adding the values in the code behind file and that works too but somehow on runtime the form doesnt pass the values back...

Sort a List<T> using query expressions - LINQ C#

I have a problem using Linq to order a structure like this : public class Person { public int ID { get; set; } public List<PersonAttribute> Attributes { get; set; } } public class PersonAttribute { public int ID { get; set; } public string Name { get; set; } public string Value { get; set; } } A person might go li...

Linq query with nullable sum problem

from i in Db.Items select new VotedItem { ItemId = i.ItemId, Points = (from v in Db.Votes where b.ItemId == v.ItemId select v.Points).Sum() } I got this query, however it fails if no vots are found with "The null value cannot be assigned to a member with type System.Int32 which is a non-nullable valu...

Opportunities to use Func<> to improve code readability

Today I finally "got" the Func<> delegate and saw how I could use it to make some of my less readable LINQ queries (hopefully) more readable. Here's a simple code sample illustrating the above, in a (very) trivial example List<int> numbers = new List<int> { 1, 5, 6, 3, 8, 7, 9, 2, 3, 4, 5, 6, }; // To get the count of those that are l...

Linq To Sql Need Dynamic Where Clause over relational tables Help?

I need Help for dynamic where clause over relational tables (one to many) in LinqToSql. User select conditions from page. (there is 4 input that user select the clauses) For example CompanyName and CompanyTitle from Customer table and OrderDate and ShipCity From Order table. But user can select one ore many of them from page interfa...

Parsing IIS configuration xml doc with linq to xml

Hi Guys, I am writing a small app that finds all the folders IIS is referencing. To do this I take the IIS config file and parse the xml looking for elements called IIsWebVirtualDir and look for the Path attribute. Here is my code XDocument doc = XDocument.Load(xmlPath); IEnumerable<XElement> elements = doc.Elements(...

Print out Linq Expression Tree Hierarchy

The DLR has some pretty cool code for Expression's, including some very nice code to print out Expression trees which I want to use so that: int a = 1; int b = 2; Expression<Func<int, int>> expression = (c) => a + (b * c) expression.Evaluate(5, stringBuilder) Outputs: (5) => a + (b * c) = 11 Where a = 1 b * c = 10 Where ...

Is LinqToSQL the same as Linq?

I was just reading about Linq to SQL being discontinued. For a while I put off learning Linq at all. Even on asp.net, when I last checked, their data access tutorials were using table adapters and bll classes. Then there are linq tutorials. Now that I have read this and that the Entity Framework is the new way to go, does that mean a...

LINQ to XML question

My requirement here is to retrieve the node that matches the hostname (for eg. machine1) and I always get back no results. Please let me know what the problem is? Thanks for the help in advance!!! XDocument configXML = XDocument.Load("the below xml"); var q = from s in configXML.Descendants("lcsetting") where ((string)s.Element("h...

LINQ for LIKE queries of array elements

Let's say I have an array, and I want to do a LINQ query against a varchar that returns any records that have an element of the array anywhere in the varchar. Something like this would be sweet. string[] industries = { "airline", "railroad" } var query = from c in contacts where c.industry.LikeAnyElement(industries) select c Any id...