linq

Anonymous classes, temporary data, and collections of anonymous classes

I'm new to anonymous classes, and today I think I ran into the first case where I felt like I could really use them. I'm writing a method that would benefit from storing temporary data inside of a class, and since that class doesn't have any meaning outside of that method, using an anonymous class sure made sense to me (at least at the ...

Nhibernate/Linq: NHibernate.QueryException : could not resolve property: Profile.class of: MyNamespace.MyObject

I'm having an issue with my linq query. I am trying to filter objects based on selected values. We use a query model which returns a System.Linq.Expressions.Expression and uses it to create an nhibernate query. Here is my linq expression. x => ( request.InitialLoad ...

Algorithm for filtering collection

I have object Country, which has Areas. Areas has Provinces. Provinces has Cities, and Cities, and Cities has hotels. I want to filter list of regions to have only objects with property userHaveBeenThere set to true(Areas,Provinces,Cities,and hotels. I'm going to bind this list to treeview. The worst part of this algorith situation, ...

Caching linq-to-sql results?

Most will recommend caching as a list. I know that is a solution and may be the one I go with. But, what if one wants to be able to treat a cached linq-to-sql with full IQueryable (IEnumerable?) functionality? If one does try to cache the raw result, an error "Query results cannot be enumerated more then once". So, to make clear my que...

filtering a DataTable using LINQ

Consider a DataTable which contains columns: RefID : string RefName : string RefDate : DateTime The DataTable does not contains any primary key. I have another List<string> named ExcludeMe. I would like to filter my DataTable and exclude all of the rows where values in RefId Column around found in the ExcludeMe list. How ca...

Using Linq to get the last N elements of a collection?

Given a collection, is there a way to get the last N elements of that collection? If there isn't a method in the framework, what would be the best way to write an extension method to do this? ...

Linq OrderByDescending ThenByDescending Issue

Hi im having a problem with ordering with linq. I have a object with two datetime fields. One just stores the date (e.g. 8/2/2010) and leaves the time as all 0's While the other datetime field is created when the record is made and has the time on it as well. The idea of these two field are because I may want to add a "Headline/article"...

LINQ to XML problem with SharePoint Web Services

I am using SharePoint Web Services to get some list items out of SharePoint for a project i am working on. I am using using LINQ to XML to parse the resulting XML to be put into a datasheet. The problem i am running into is when trying to parse an item that isn't required in SharePoint... var fields = from item in results.Descendants...

Linq to Sql DatabaseExists

Hi.... Lets say i have a database in c:\database.mdf what is the difference between using context.DatabaseExists("c:\database.mdf") or just simply checking with File.exists("c:\database.mdf") ? i ask this because i get strange behavior form DatabaseExists method : it tells me sometimes that c:\database.mdf exists but it's not there, ...

How to select same column's name from different table in Linq?

I've 2 tables with same column's name, for example, both table A and table B has column's name "Test". I want to select column Test from both table A and B to entity class. How can I do this? ...

linq to xml CDATA problem

When I used linq to retrieve xml, <CDATA[[ ]]> is removed, which is what I want to achieve. But when I save the file, <CDATA[[ ]]> becomes &lt;CDATA[[ ]]&gt; in the xml file. Whether or not I explicitly append with <CDATA[[ ]]> before saving gives me the same result. Tried appending with &lt;CDATA[[ ]]&gt; but still gives me th...

Adding OR expressions in a loop in Linq

I have a variable number of OR conditions that I want to put together into one Linq query. How do I do this in a loop? Basically, the final query is to be: IQueryable<MyObject> Q; Q = Q.Where(q => (condition1) || (condition2) || ..... || (condition N)); Something like: For (int i = 0; i < someNumber; i++) { Q = Q.Where(q => (exis...

selecting an element in xml using linq extension mehtods

Hi, I am little new to linq and was wondering how i can select the application in the following xml based on the application name using Extension Methods (not using the query expression) <applicationlist> <application> <name>test1</name> <ele1>852</ele1 <ele2>http://localhost/test1&lt;/ele2&gt; </application> <application>...

Listing Count per "Category" for IQueryable

Hello I have an mvc-page that lists categories, and I would like to have it to count the number of products per category. It's IQueryable. <% foreach (var item in Model.Categories) { %> ..... <td><%: item.CategoryName %></td> <td><%: item.Products.Count() %></td> It lists: 5 5 Instead of: 3 2 Any Ideas how I can "partition" it per ...

Select new object as a parameter while preserving it's run-time-generated type

Consider the following: // select a subset of the DataTable var subset = DataTable.Where(...).Select(row => new { Id = Convert.ToInt32(row["Id"]), Name = row["Name"].ToString(), Email = row["Email"].ToString() }); // or create a new object var subset = new { Id = 1, Name = "something random", Email = "name@domai...

How can i shorter my linq codes?

hi; i try to run my codes. My program more slowly runnig. i need to give performance also write less codes in GetAliSpReqs(), GetMaintData();GetAccess....GET(... How can i write more effective below codes. They are too slow also not useful. forexample i try to write les than 1-2 line with GetAliSpReqs()? How can i ? please help me... ...

How to loading XML files from a file

my xml is bellow: <Demo> <ClientCompanyId CompanyId="1"> <MyMenu> <module Text="Basic Settings" ModID="Mod1" ModuleID="1" MenuType="0" Perm="False"> <menu Text="Forms" MID="1-1" ParentID="Mod1" MenuDescription="Mod" ModuleID="1" MenuType="0" Perm="False"> <Leaf Text="LookUp" MID="1-3" ParentID="1" MenuDescription="" ModuleID="1" MenuTyp...

cloning object on linq grouping - copy constructor substitute

Hi all, I have collection of custom objects(assets) which I want to group with LINQ. Custom object has standard properties like id, name and cost property. When grouping I want to calculate cost for each group, so I'm using little trick like this: from a in assets group a by a.AssetId into ga select new Asset() { ...

How to get attribute names of element in xml by LINQ in C#

Hi, I have xml element: <.SECTIONS> <.SECTION ID ="1" NAME="System Health" CONTROL-TYPE="Button" LINK="http://www.google.co.in/"> <.DATAITEMS> <./DATAITEMS> <./SECTION> <./SECTIONS> I want to get the all attribute names of SECTION Element. as ID,NAME,CONTROL-TYPE,LINK at server side using ...

How can I get a List from all nodes in a tree using LINQ?

Hi All, How can I get a List from all nodes in a tree using LINQ? My classes are: class Node { public class Node() { Children = new List<Node>(); } public List<Node> Children { get; set;} } class Tree { public Tree() { Roots = new List<Node>(); } List<Node> Roots { get; set;} } ...