linq

Loading contents of file into a collection

If I have a text file, which say is comma delimited and has several values per line, how could I use LINQ to insert this into a collection in an efficient manner (ie avoid loading the whole file (assume the file is large) at once when there may be a better way). I am using .NET 4.0 (VS2010 Beta 2). I know using the string's split method...

LINQ Query - Odd Result - OrderBy

Hello, I have the following code: int pageNumber = 0; int pageCount = 10; int numberOfRecords = 10; var query = V_ers_notice.Skip(pageNumber * pageCount).Take(numberOfRecords ); return query.OrderByDescending(id => id.DOC_DATE).Select(ERSNoticeMap.DataToObject).ToList()); The ERSNoticeMap code is as follows: public class ERSNo...

Hashset vs. IQueryable

Recently I was working on implementing a small snippet that caches my results and the way I was doing it was using a Dictionary as follows: private Dictionary<ID, IQueryable<Results>> _simpleCache; The idea was to search for all the results that have the id specified by 'ID' and if the Dictionary contains the key == id, we simply sea...

LINQ to XML: suppressing redundant namespace attribute in child nodes

If a node belongs to a namespace, it's children by default belong to the same namespace. So there's no need to provide an xmlns attribute on each child, which is good. However. If I create two nodes like this: Dim parent = <parent xmlns="http://my.namespace.org"/&gt; Dim child = <child xmlns="http://my.namespace.org"&gt;value&lt;/chil...

How to use where condition

can we dynamically appened where condition on a linq query? for example: class Result { string v1; string v2; string v3; } List<Result> result = (from r in results select r); //i want to do something like the following.... if(conditionA) { result = result appened (or v1 = xxx) } else if(conditionB) { resul...

How do I compose Linq Expressions? ie Func<Exp<Func<X, Y>>, Exp<Func<Y, Z>>, Exp<Func<X, Z>>>

I'm creating a Validator<T> class. I'm attempting to implement the Linq SelectMany extension methods for my validator to be able to compose expressions using a Linq query and validate the final result even when the underlying values change. The following test code demonstrates my intent. var a = 2; var b = 3; var va = Validator.Create...

How to Get Font Properties from a Font File Name?

I'm trying to use Linq to loop through all fonts in the %windir%\Fonts folder and find the one that has a property title of "Arial" (or any Font Family name supplied), but I can't seem to access the font properties (things like "Title", "Font style", "Designed for", etc.). The following is only giving me the basic file info: Dim f...

using stored procedures as functions from edmx file

hi, i have an entity-model file (edmx) file which contains few tables and few stored procedures. how can i call those stored procedures that are mapped to functions? i thought it should be trivial, and i do see the mapping in the edmx file, but i can't figure how to use it in the code. here is one mapping example: <Function N...

Linq Date Performance Death

Hi, I have a database that stores dates broken into int's. I.e. Day, Month and Year are stored separately in different columns. In existing SQL queries, the method used is: DATEADD(dd, - 1, DATEADD(mm, Z.LastMonth, DATEADD(yy, Z.LastYear - 1900, 0))) AS last_date I cannot change the database to store dates instead. Looking further i...

Finding if a target number is the sum of two numbers in an array via LINQ

A basic solution would look like this: bool sortTest(int[] numbers, int target) { Array.Sort(numbers); for(int i = 0; i < numbers.Length; i++) { for(int j = numbers.Length-1; j > i; j--) { if(numbers[i] + numbers[j] == target) return true; } } return false; } Now I'm v...

LINQ to XML, replace child nodes but keep state

I've got the following XML passed as an IQueryable to a method, i.e. XML: <body> <p>Some Text</p> <p>Some Text</p> <pullquote>This is pullquote</pullquote> <p>Some Text</p> <p>Some Text</p> <body> Method: public static string CreateSection(IQueryable<XElement> section) { var bodySection = from b in articleSection.Des...

LINQ to SQL: "String or Binary Data Would Be Truncated" error

I know there are several questions like this on here already, but I can't find one that relates to my problem. I've got an SP declared like this: CREATE PROC [dbo].[SomeProc] ( @param1 VARCHAR(255) , @param2 INT , @param3 VARCHAR(8) ) When I add the Stored Procedure to the Data Model it generates the following signature: int S...

.Net Framework DataTable.Select(String) method when the filter expression contains ' or "

In a .Net web application I use the public DataRow[] Select(string filterExpression) method in many places. Due to a last minute change characters such as ' and " are now valid input. What options do I have, is there a way to change the filterExpression in an adequate way and still preserve the Select functionality on the datatable, can...

LINQ or DataTable when number of fields unknown.

Hi, I have a dynamic query which returns a DataTable. This DataTable will contain a varying number of columns depending on the selection used in the query. I want an efficient way of filtering the records in the DataTable. For Example The DataTable contains columns : A, B, C and D I need to query this data at serveral points in my ...

What is the linq equivalent to the SQL IN operator

With linq i have to check if a value of a row is present in a array. The equivalent of the sql query: WHERE ID IN (2,3,4,5) How can i do? thanks ...

NHibernate Subquery Linq - How to select groups that contain a certain item by id

Short Version This query works in the database but fails with Linq To NHibernate. Why? var items = (from g in db.Find<DataGroupInfo>() where (from d in g.data where d.Id == dataID select d).Count() > 0 select g).ToList(); Detailed Long Version I have two objects mapped by NHibernate Automapper with a Ma...

Split a List of structs into sublists based on the stucts contents

I have a List that I need to split into sublists, one for each value of MyStruct.GroupingString. Can I do this via linq? ...

Help to convert to linq

Hi can someone help to convert this query to linq?? SELECT DISTINCT Users.IDUsr, Users.Name FROM [UserGroups.UnitType] INNER JOIN [UserGroups.Units] ON [UserGroups.UnitsTypes].IDUGOUT = [UserGroups.Units].IDUGOUT RIGHT OUTER JOIN Users INNER JOIN [Users.Groups] ON Users.IDUsr...

how to group by over anonymous type with vb.net linq to object

I'm trying to write a linq to object query in vb.net, here is the c# version of what I'm trying to achieve (I'm running this in linqpad): void Main() { var items = GetArray( new {a="a",b="a",c=1} , new {a="a",b="a",c=2} , new {a="a",b="b",c=1} ); ( from i in items group i by new {i.a, i.b} into g le...

IQuerable remove from the collection, best way?

IQueryable<SomeType> collection = GetCollection(); foreach (var c in collection) { //do some complex checking that can't be embedded in a query //based on results from prev line we want to discard the 'c' object } //here I only want the results of collection - the discarded objects So with that simple code what is the best way...