linq

How To Define The Return Type In A Function With LINQ?

I would like to know how to define a returntype in a function in following situation. I've got a products and I was returning all information or one product at a time. as you can see in my function defined below. public static Products GetProducts(int pid) { var pro = from p in context.Products select p; if(pid...

How do I specify the sorting field and direction in a linq query at runtime?

Lets suppose that I have the following simple query var q = from p in products orderby p.ProductName descending select p; What would be the simplest and most straightforward way to specify the sort field and direction at runtime? ...

What is the best way to check two List<T> lists for equality in C#

There are many ways to do this but I feel like I've missed a function or something. Obviously List == List will use Object.Equals() and return false. If every element of the list is equal and present in the same location in the opposite list then I would consider them to be equal. I'm using value types, but a correctly implemented Dat...

Enum with default typecast? is that possible?

is it possible to make a default typecast for an enum? I use enum for a lot, such as states and I want to compare enums directly to LINQ fields, but I have to typecast all the time. ...

Linq PredicateBuilder - Multiple ORs - Newbie Q

Hi - another day another LINQ newbie Q! I'm trying to use the PredicateBuilder, as described here - http://www.albahari.com/nutshell/predicatebuilder.aspx The following code var predicate = PredicateBuilder.False<StreetDTO>(); predicate = predicate.Or(p => p.Locality.Contains(criteria.Locality)); predicate = predicate...

Linq - Excluding items from different list types.

Is there any way to select items in a list that aren't contained in another? For example: list1 = From t In list1 Where Not list2.Contains(t.column1) That gives me the error: Value of type 'Integer' cannot be converted to '<anonymous type>' which makes sense, since list2.Contains is expecting the same type as list2. However, the li...

Debugging a C# Object Initializer

Does anyone have any tips for debugging exceptions in a C# object initializer block? The object initializer syntax is basically all or nothing, which can make it especially difficult to troubleshoot inside of a LINQ query. Short of breaking the object creation out to a separate method, is there anything I can do to see which property set...

Making Linq To SQL DRY.

We decided to use Linq To SQL for our Data Layer on our most recent project. We have a functional solution, that so far has handled everything we have thrown at it, with one major problem. We have to code the same method over and over again to retrieve just slightly different result sets from our database. As an example: pu...

Dynamic "Not" by parameter in LINQ (Or any other code for that matter)

Okay, This may be really simple or it may not even be possible, or I am just having a brain freeze :) Here is an example of what I am trying to do: public void SomeMethod(bool include) { using (AccountDataContext db = AccountContextFactory.CreateContext()) { if (include) ...

Linq .Any VS .Exists - Whats the difference?

Using Linq on collections, what is the difference between the following lines of code? if(!coll.Any(i => i.Value)) and if(!coll.Exists(i => i.Value)) Update 1 When I disassemble .Exists it looks like there is no code. Update 2 Anyone know why there is not code there for this one? ...

How to get an IEnumerable<T1> of members of the class T2 in a Enumerable<T2> using Linq? (C#)

Suppose I have: public class foobar { public int lorem; public int ipsum; } IEnumerable<foobar> items = new List<foobar>(); items.add(new foobar(){lorem = 0, ipsum = 0}; items.add(new foobar(){lorem = 1, ipsum = 1}; How can I get a IEnumerable of all "lorem" in "items" using Linq? ...

Entity Framework - "Unable to create a constant value of type 'Closure type'..." error

Why do I get the error "Unable to create a constant value of type 'Closure type'. Only primitive types (for instance Int32, String and Guid) are supported in this context." when I try to enumerate the following Linq query? IEnumerable<string> searchList = GetSearchList(); using (HREntities entities = new HREntities()) { var myList = ...

Can I use predefined namespaces when loading an XDocument?

I often have to deal with XML documents that contain namespaced elements, but doesn't declare the namespace. For example: <root> <a:element/> </root> Because the prefix "a" is never assigned a namespace URI, the document is invalid. When I load such an XML document using the following code: using (StreamReader reader = new Stream...

Entity Framework - "All" method

The All method is supposed to evaluate the argument against all elements in the list. It works ok in regular Linq but when I try to use it with EF it throws an error ("Unable to create a constant value of type 'Closure type'. Only primitive types (for instance Int32, String and Guid) are supported in this context. ") Example: var myLi...

join query with linq

I am trying here to make a few left joins into a linq query but I'd say I rather have no idea how to materialize this idea. Basically here is the 3 database structures I want to play with. <tags> id | name <events_tags> tag_id | event_id <events> id | name | some-other-fields so for each events there is a one-to-many relation with ...

linq orderbyAscending?

Hi I am using the following to sort results of a datatable returned by a tableadapter Dim spots = myDataTable.Where(Function(t) t.UserID = 1).OrderByDescending(Function(t) t.Title) The thing is, I also need to OrderByAscending the very same datatable. But as far as I see, it is not giving it as an option. I am sure there is a way to ...

Stuck on a subquery that is grouping, in Linq`

Hi folks, I have some Linq code and it's working fine. It's a query that has a subquery in the Where clause. This subquery is doing a groupby. Works great. The problem is that I don't know how to grab one of the results from the subquery out of the subquery into the parent. Frst, here's the code. After that, I'll expplain what piece o...

LINQ options.loadwith problem.

I am writing a tag-based ASP.net system. Using the following db scheme: Topic <many-many> TagTopicMap <many-many> Tag Basically it is a 3NF approach (toxi) that I found from the following: http://www.pui.ch/phred/archives/2005/04/tags-database-schemas.html Here is the code snippet I have: DataLoadOptions options = new DataLoadOptions...

Applying LINQ to Objects Group By and Sort By to generic List<T> (C#)

I am having a hard time creating working Group By and Sort By clauses against a generic List myList. myList has a list of property 'Settings' which itself contains a list of 'child' properties for each business. I want to group by a Industry and within each Industry, sort by business name. My intent would be this: string groupSetting...

Linq Error

Get value out of DateTime column if null to return String.Empty else DateTime.ToShortDateString What am i doing wrong => query produced below: var queryable = from p in Products select new { selldate = p.SellEndDate == null ? string.Empty : p.SellEndDate.Value.ToShortDateString() }; Error: InvalidOperatio...