linq

Filter a list of objects using LINQ

I have a list of custom objects. These objects have 2 datetime properties on them. I need to get a list of unique (ignore the time part) datetime objects from the two objects properties. Example I have 2 objects with 2 datetime properties: object1.date1 = "01/01/2001 12:54" object2.date1 = "01/02/2001 12:51" object3.date1 = "01/01/20...

How-to Optimize this LINQ Extension Method

Hi, I wrote a custom ordering LINQ extension method as below but I think it can be optimized for large results. Here is the code : public static IEnumerable<T> OrderByAncesty<T>(this IEnumerable<T> source, Func<T, DateTime> dateSelector, Func<T, float> scoreSelector) { var original = source.ToList(); var maxDat...

Aggregate vs. Any, for scanning objects such as IEnumerable<bool>

Just wondered if any LINQ guru might be able to shed light on how Aggregate and Any work under the hood. Imagine that I have an IEnumerable which stores the results of testing an array for a given condition. I want to determine whether any element of the array is false. Is there any reason I should prefer one option above the other? ...

Help with LINQ query

I'm trying to do this with LINQ: SQL>> SELECT p_Id from items WHERE p_Name = 'R1' LINQ>> var getpID = (from p in myDatabaseDataSet.Items where p.p_Name == ptxtBox.Text select p.p_Id).SingleOrDefault(); int p_Id = getpID; But getpID always return 0. What's wrong?. E...

What does this mean in C# or Linq - ( () => )

I was going through Jeffrey Palermo book and came across this syntax. private void InitializeRepositories() { Func<IVisitorRepository> builder = () => new VisitorRepository(); VisitorRepositoryFactory.RepositoryBuilder = builder; } Any help? ...

Case insensitive string compare with linq-to-sql and linq-to-objects

see also Differences between LINQ to Objects and LINQ to SQL queries We are using the some queries over our database and our in memory objects. What is the best way of doing an insensitive string compare with linq-to-sql so that? It runs fast on SQL Server The same query expression can be used with linq-to-objects to get the same res...

Unpassable Where Clauses LINQ-to-SQL

As I'm struggling to learn LINQ I’ve managed to generate a SQL statement with "AND (0 = 1)" as part of the where clause. I'm just wondering if this result is common in poorly written queries and is a known issues to try and avoid or if I am doing something totally backwards to end up with this. Update public static IEnumerable<ticke...

LINQ question with string split and arrays in csv files

Given the code below: var query = from line in ClientCount() let aLine = line.Split(",") where aLine.GetValue(1) != 0 select aLine; To query a csv file, will aLine.GetValue(1) get the 1st line (after 0) in the .csv file, or 1st string, eg: abc, def And therefo...

Is there a way to do the google did you mean in linq?

I have list of words. I type in a word misspelled. Can I query the list using linq to get words that sounds like (soundex) the misspelled word? ...

IList with an implicit sort order

I'd like to create an IList<Child> that maintains its Child objects in a default/implicit sort order at all times (i.e. regardless of additions/removals to the underlying list). What I'm specifically trying to avoid is the need for all consumers of said IList<Child> to explicitly invoke IEnumerable<T>.OrderBy() every time they want to...

What am I missing in this chain of predicates?

NOTE: Right before posting this question it occurred to me there's a better way of doing what I was trying to accomplish (and I feel pretty stupid about it): IEnumerable<string> checkedItems = ProductTypesList.CheckedItems.Cast<string>(); filter = p => checkedItems.Contains(p.ProductType); So OK, yes, I already realize this. However, ...

Processing ranking data w/ C# & LINQ

I have an object in a list that I need to rank several different ways. Currently the code is rather unwieldy as it requires me to individually address each column. Example: public class Data { public int AValue { get; set; } public int ARanking { get; set; } public int BValue { get; set; } public int BRanking { get; set;...

C# Linq over XML => Lambda Expression

I have an xml document which consists of a number of the following: - <LabelFieldBO> <Height>23</Height> <Width>100</Width> <Top>32</Top> <Left>128</Left> <FieldName>field4</FieldName> <Text>aoi_name</Text> <DataColumn>aoi_name</DataColumn> <FontFamily>Arial</FontFamily> <FontStyle>Regular</FontStyle> <Fon...

LINQ Select Statement

Hi All, I'm developing a web application myself to follow some rssfeeds. My purpose is just learning some ajax and i send data from server to client side in JSON format. I use LINQ for querying data and JSON .NET API for object to string Serialization in the server side. The problem is that because of there exists foreign keys between ...

C# LINQ Joining And where statement not working

I am trying to have the collection of order IDs be used in my where statement how come i can't get this to work? List<int> orders = new List<int>(){1,2,3,4,5}; DataTable dtTable1 = getOrders(); DataTable dtTable2 = getOrderDetails(); var results = from a in dtTable1.AsEnumerable() join b in dtTable2.AsEnumerable() on a....

C# Linq question: How to identify and count items in a set

I have a friend who is using c# Linq to report on the following table: varchar(50) name datetime when The customer wants to pick what columns to summarize, like Total for this year, Total for Last year, Total for two weeks ago, etc. Each column would have some begin and end time they summarize with a count. I did a quick select s...

C# SQL Statement transformed TO LINQ how can i translate this statement to a working linq

I am having trouble with this I have 3 Data tables i use over and over again which are cached I would like to write a LINQ statement which would do the following is this possible? T-SQL VERSION: SELECT P.[CID],P.[AID] ,B.[AID], B.[Data], B.[Status], B.[Language] FROM MY_TABLE_1 P JOIN ( SELECT A.[AID], A.[Data], A.[Status], A....

Using a custom Linq Data Context with NULL DateTime field...?

I have a custom DataContext with a Table member... Entry is defined as... [Table(Name="Entry")] public class Entry { [Column] public DateTime MyDate { get; set; } } The MyDate field in the Entry table can be NULL. I have a very simple Linq query that retrieves all records in Entry but when it encounters a NULL MyDate field, I re...

Remove entries from dictionary using LINQ - locking

Hi I have a static dictionary which holds authentications for a web service. I'm running a timer (hence the TimerCallBack sig on the method below) to remove those authentications which have been dormant for a period. I'm not quite sure of the locking behaviour I need in relation to linq. Here's my code thus. I'm slightly concerned th...

Mux & Demux w/ LINQ

I'm playing around with using LINQ to Objects for multiplexing and demultiplexing but it seems to me that this is a pretty tricky problem. See this demuxer signature: public static IEnumerable<IEnumerable<TSource>> Demux<TSource>(this IEnumerable<TSource> source, int multiplexity) On an abstract level this is easy but ideally one wou...