linq

Access the value of a member expression

If i have a product. var p = new Product { Price = 30 }; and i have the following linq query. var q = repo.Products().Where(x=>x.Price == p.Price).ToList() In an IQueryable provider, I get a MemberExpression back for the p.Price which contains a Constant Expression, however I can't seem to get the value "30" back from it. Update I...

C# select elements from IList

I have a list of objects, IList<O>. O has several properties but only two of them are relevant: Date and Duration. I want to "split" the list into several lists that contain only the objects that have matching Date and Duration Properties. Example: 0- Date==1, Duration==7 1- Date==1, Duration==7 2- Date==2, Duration==7 3- Date=...

Linq: How to calculate the sales Total price and group them by product

I have a order list and I want to generate and rank the product with its total sales and quantity. With @tvanfosson's help, I can bring the grouped product detail with the following code, but how can I calculate and add up the total sales and quantity into each productListResult's object? Can anyone help me with this? Many thanks. ...

How to dynamically choose two fields from a Linq query as a result

If you have a simple Linq query like: var result = from record in db.Customer select new { Text = record.Name, Value = record.ID.ToString() }; which is returning an object that can be mapped to a Drop Down List, is it possible to dynamically specify which fields map to Text and Value? Of course,...

XML construction in C#

Currently i need to construct and update large xml files. So what is the best C# xml parser to create or update the xml files? I heard about LINQ in c#, can this be used? ...

How to perform a many-to-many Linq query with Include in the EF.

Hi, I don't know how to perform this query using Linq and the EF. Imagine I have three tables A, B and C. A and B have a many-to-many relationship. B and C have a 1-to-many relationship. I want to obtain records from B including C but filtering from A's Id. I can get easily the records from B: var result = Context.A.Where(x => x.Id.E...

LINQ "Except" Operator

i have a list of event Ids that i want to be excluded from my select statement, but no sure how to implement this: this is what stores my list of event Ids List<int> ExcludedEvents; and this is my select statement (from an XML feed) var allEvents = from eventsList in xmlDoc.Elements("shows").Elements("Show") select n...

VS 2008 ADO.NET Entity Data Model questions

I am learning L2E and have two questions: when I add ADO.NET Entity Data Model to my VS 2008 C# project. VS will add a cs file for you and the default namespace will be your project name, saying SystemSoftware in my case. Can I config VS to create a different namespace, such as MyCompany.SystemSoftware, for me or I have to manually cha...

How to apply global filter on Entity Framework?

I have a table in my model named Customers with a field IsActive. Whenever I run a query on Customers, only the active customers should be retrieved. I can include the filter in every query, but that doesn't look very. I would like to be able to override the Customers property at the Object Context lever, but I am not sure if this is pos...

WCF Data Services - neither .Expand or .LoadProperty seems to do what I need

I am building a school management app where they track student tardiness and absences. I've got three entities to help me in this. A Students entity (first name, last name, ID, etc.); a SystemAbsenceTypes entity with SystemAbsenceTypeID values for Late, Absent-with-Reason, Absent-without-Reason; and a cross-reference table called Stude...

LINQ to SQL -Internals -C#

Does LINQ to SQL work in connected environment or disconnected environment? I mean if you compile the query it builds expression query and the query is sent down to sql server ,there it is translated into T-SQL statement and executed and the final result is sent back to the C# code.I hope it is working in connected environment.Is there a...

Anonymous Methods / Lambda's (Coding Standards)

In Jeffrey Richter's "CLR via C#" (the .net 2.0 edtion page, 353) he says that as a self-discipline, he never makes anonymous functions longer than 3 lines of code in length. He cites mostly readability / understandability as his reasons. This suites me fine, because I already had a self-discipline of using no more than 5 lines for an ...

Rewriting a statement using LINQ(C#)

Is it possible to write the folowing using lambda(C#) private static void GetRecordList(List<CustomerInfo> lstCustinfo) { for (int i = 1; i <= 5; i++) { if (i % 2 == 0) lstCustinfo.Add(new CustomerInfo { CountryCode = "USA", CustomerAddress = "US Address" + i.ToString(), CustomerName = "US Customer Name" + i...

Getting Dictionary<string,string> from List<Control>

I want a dictionary containing the names and text of all controls. Is it possible with predefined framework methods/LINQ/colection initializers or do I have to make a loop and add all entries by myself? This gives me an error message: List<Control> controls; // .. initialize list .. controls.ToDictionary((Control child,string k)=>new K...

Dynamic Linq Property Converting to Sql

I am trying to understand dynamic linq and expression trees. Very basically trying to do an equals supplying the column and value as strings. Here is what I have so far private IQueryable<tblTest> filterTest(string column, string value) { TestDataContext db = new TestDataContext(); // The IQueryable data to query...

Cast problem with LINQ

I'm tring to get my product's types to a list with Linq. var types = (from t in NHibernateSession.Linq<Product>() select t.ProductType).Distinct().ToList<ProductType>(); return types; But its giving an Unable to cast object of type error '...Domain.Product' to type '...Domain.ProductType'. ProductType is a pro...

LINQ to XML Cloning

Can anyone explain why the original address XElement street node changes? It looks like customer1 holds a reference to the address XElement but customer2 and customer3 have taken copies. Why did the original address change? (LINQPad example) var address = new XElement ("address", new XElement ("street", "Lawley St"), ...

Is it possible to enumerate for all permutations of two IEnumerables using linq

I could do this using loops, but is there a way to take two IEnumerables, enumerate through all possible permutations and select an object that contains the permutation? I feel like this 'should' be possible but am not really sure what operators to use. Thanks James ...

C# check an element exists while using LINQ to XML

OK, bit of a random question, but the best way to do this is to just add the code, you'll be able to see what I mean straight away: XML: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <id>1</id> <name>Blah-face</name> <Type>1</Type> </customer> <customer> <id>2</id> <name>Blah-face-2</name> ...

Transform Two Lists into One Different List

I have two lists of double List<double> X List<double> Y And I have a destination object: public class PointD { public double X {get;set;} public double Y {get;set;} } How to transform them into a single list? public static List<PointD> Transform(List<double> X, List<double> Y) { } All the errors checking must be there...