linq

Help Linqifying collection to Dictionary

I'm refactoring this code and was trying to think of a simple linq expression to populate this Dictionary. IEnumerable<IHeaderRecord> headers = PopulateHeaders(); var headerLocationLookup = new Dictionary<string, IHeaderRecord>(); foreach (var header in headers) { //destination locations can repeat, if they do, dictionary should only ...

Eager loading / prefetching many-to-many without LoadOptions - Linq to Sql

I've got a situation where I need to prefetch some entities through a many-to-many relationship. So it's like the classic BlogPost <- BlogPostTag -> Tag situation. Yes, I'm aware of LoadOptions but I can't use it because it's a web application and I'm using the one datacontext per request pattern. It also seems you can't use projection...

User defined filters/queries?

I'm trying to figure out a way to allow users of my application to define their own queries or filters that can be applied to a collection. I want to eventually provide an intuitive user interface to create these filters (see image below), but initial it would be OK if a user had to type of a text query string. I'll also need to be able ...

Easiest Way to get id or link and updated date from a RSS feed?

I tried to use linq but failed because i have no xml knowledge. What is the easiest way to get the id or link and updated from an rss feed? (an example feed -> http://stackoverflow.com/feeds/question/1926343) I have no idea how to skip all elements until the first entry element. I also dont know how to pull each link and data from each...

LINQ: Getting the row with the maximum value of a given attribute

I have a bunch of rows grouped on an attribute called MyID. Now I want the one row from each group where the StatusDate attribute is the highest in that one group. This is what I've come up with. rows.Select(x => x.Where(y => y.StatusDate == x.Max(z => z.StatusDate)).First()) With a bit more explanation: rows.Select(x => // x is a g...

Checks nulls using LINQ

Hi all, I have this code. How can I check null values for SingleOrDefault method ?? public static List<ETY.Rol> GetRolesByApplicationAndCompany(this UsuarioContext usuario, int company, int app) { List<ETY.Company> lCompanies= usuario.Companies; var roles = lCompanies. SingleOrDefault(e ...

linqdatasource: Databind from another project

I have a solution "Foo" with 2 projects. "FooCore" and "FooWeb" where FooCore peoject contains the FooDatacontext in namespace Foo.FooCore.Core.Domain . How can bind the datacontext with linqdatasource in FooWeb project in aspx page. Is this possible by doing "<% Import Namespace="Foo.FooCore.Core.Domain"%>" in that aspx page? I hope I a...

LINQ to XML problem with reading XML in C#

Hi - I am trying to read an XML file using LINQ. I have had no problem reading and parsing simple XML files but this one has me stumped. Here is a portion of the file: The file is properly formed and valid. <Activities> <Activity Sport="Other"> <Id>2009-12-17T19:53:14Z</Id> <Lap StartTime="2009-12-17T19:53:14Z"> ...

Return one result from LINQ Query

if you have a select LINQ query that should only return one result, do you have to have a foreach loop to get the result? Or is there a better way? ...

Linq Query To Combine 2 Lists

I have 2 lists and I need to combine the joining values from A and B, but also include the values from A and B that don't match the join. class TypeA { public string Key { get; set; } public int ValueA { get; set; } } class TypeB { public string Key { get; set; } public int ValueB { get; set; } } class TypeAB { pub...

LINQ distinct on class item?

Note this question is similar this one except I'm not working with linq-to-sql, so the "let" is not usable. Basically I have a select of the type ... .Select(c => new SampleClass { Id = c.Location.Name, Name = c.Location.Name }).Distinct().ToList() which used to work when I just had ... .Select(c => c.Location.Name).Distinct().ToLis...

Linq query with a Join, not returning the correct results.

Hi Folks, i've got the following code, which compiles but doesn't retrieve the correct results. I'm trying to retrieve all the Banned log entries for people who have been recorded at cheating on a gaming server. The database (in this case, two IList tables) has two simple tables. GameFiles : the game which has a log file .. which w...

Linq Inconsistency

Can anyone help explain why when I bind the following code to a gridview, the negative numbers come out as 0.00? var shoppingCartItems2 = Checkout.GetPropertyListingShoppingCartItems(SC.ShoppingCartID); var columns = from sci in shoppingCartItems2 select new { Description = sci.ShoppingCartItemType...

Linq Fake/Mock context

Let say I got the following code in my SqlUserRepository : var user = from u in NHibernateLinqContext.Linq<User>() ... What I would like is to have a similar context for my FakeuserRepository var user = from u in FakeLinqContext.Linq<User>() ... Like that, I'll be able to use the same logic in my SqlUserRespository that my FakeUser...

Linq-to-SQL throwing NullReferenceException despite checking for null arguments

I'm trying to write Linq-To-SQL queries in LinqPad to help migrate users from old tables to new tables. Part of this migration is storing all addresses in a separate table. I'm using the following query to determine if a user's address exists in the new table (so I don't have duplicate entries): var addresses = from a in Addresses where...

Linq Table to DataTable casting

How to cast System.Data.Linq.Table to System.Data.DataTable DemoDBDataContext context = new DemoDBDataContext(); DataSet ds = new DataSet(); var query = context.Customers; ds.Tables[0] = query; How to do that? the assignment ds.Tables[0] = query; It throws Property or indexer 'Sys...

Can I do this better using LINQ

List<byte[]> data = new List<string>(File.ReadAllLines(Filename)).ConvertAll<byte[]>(delegate(string value) { return new List<string>(value.Split('.')).ConvertAll<byte>(delegate(string byteVal) { return Convert.ToByte(byteVal); }).ToArray(); }); ...

Linq 'equals' keyword Revisited - Does it compare values and references to objects?

This question is a follow on to link text hence the revisited in the title. I raise this as a new question as the accepted answer and comment made under the original question both suggest that the equals keyword used in the join query refers to only value types within the comparison. I believe this is misleading as follows. Behind the s...

LINQ, Where() vs FindAll()

Hi, can someone explain how the LINQ functions Where(..) and FindAll(..) differ? they both seem to do the same thing.... ...

LINQ: How to convert the nested hierarchical object to flatten object

Hello, How to convert the nested hierarchical object to flatten objects by using LINQ? I know that we can easily use foreach loop to achieve that. But I'm wondering if there is a way to write it in LINQ. class Person{ public int ID {get;set} public string Name {get;set} public List<Person> Name {get;} } Data : ID : 1 Nam...