linq

Linq for NHibernate - filtering on <many-to-one> foreign key causes extra lookup

Trying out Linq for NHibernate and first noticed this: var list1 = (from ut in session.Linq<UserThings>() where ut.User.ID == 10 select ut).ToList(); var list2 = session.CreateCriteria(typeof (UserThings), "ut") .Add(Expression.Eq("ut.User.ID", 10)) .List<UserThings>(); This first query will join on the 'User' ...

Change app.config connection string depending on PC

Hi guys I'm doing a c# course at university, and now I'm beginning linQ to xml, linQ to Sql-Server, etc. I work with the example projects in two PCs (university PC and office PC) Is there an easy way to change connection string (from app.config) at runtime, or designtime (using a constant in main.cs) so I can use a connection string at u...

Import txt to sql database via linq

i need to import a large tab deliminated text file with a lot of columns (over 50 columns) i would like to write a c# script that creates the table based on the header of the text file. assume all fields are nvarchar(1000) i cannot use any program such as sql data import wizard. ...

How to select/filter against substring in a list of strings?

I have a LINQ result set I'm trying to filter in a strange and peculiar way. List<string> MyDomains = [Code to get list]; var x = (from a in dc.Activities where a.Referrer != null && a.Referrer.Trim().Length > 0 && !a.Referrer.Contains("localhost") && a.SearchResults.Count() == 0 orderby a.ID descendin...

Where do I put the "orderby group.key" in this LINQ statement?

this code: string[] words = {"car", "boy", "apple", "bill", "crow", "brown"}; var groups = from w in words group w by w[0] into g select new {FirstLetter = g.Key, Words = g}; //orderby ???; var wordList = groups.ToList(); //var wordList = groups.ToList().OrderBy(???); wordList.ForEach(group => { Console.WriteLi...

Multilpe object initializers in 1 LINQ query

I was wondering how I could select multiple object initializers in 1 select statement using an XML document. I would like to avoid multiple iterations on the same file. The XML structure looks like this: <root> <doc name="test.doc"> <version lang="nl"> </version> <version lang="fr"> </version> </doc> <doc name="t...

How can I define variables in LINQ?

This code: string[] files = {"test.txt", "test2.txt", "notes.txt", "notes.doc", "data.xml", "test.xml", "test.html", "notes.txt", "test.as"}; files.ToList().ForEach(f => Console.WriteLine( f.Substring( f.IndexOf('.') + 1, f.Length - f.IndexOf('.') - 1 ) )); produces ...

LINQ Select from sub-list

Hi, i currently have the problem that i don't know how to make a LINQ select to grab ALL Productpricediscounts from a category: public class ProductCategory { public List<Product> categoryProducts; } public class Product { public List<Productprice> productPrices; } public class Productprice { public List<Productpricedisco...

XElement.Elements() extension method?

Hello all I have an XML file that is loaded into an XElement. I want to count the number of PollEvent children that both exist and that satisfy some conditions. I have got the code working to count the number of PollEvents in total but when I come to filter this list I do not seem to be able to call .Where, I believe this to is due to...

How can I turn this 12-line method into a 1-line LINQ expression?

How can I replace ConvertListToString(extensions) with an elegant LINQ statement? using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestExtn2343 { class Program { public static void Main(string[] args) { string[] files = { "test.txt", "test2.txt", ...

Linq include with where clause

Hey so I've got the situation where I'm pulling a client back from the database and including all the case studies with it by way of an include return (from c in db.Clients.Include("CaseStudies") where c.Id == clientId select c).First(); but what I want to do now is and a where clause on the included casestudies so tha...

Is there an advantage to USING vs. declaring a context variable?

These two snippets do the same thing - is there one that's better than the other, or is it just a matter of preference? Using context As MyDatabaseDataContext = New MyDatabaseDataContext() Dim test = context.Employees.Count End Using vs. Dim context As MyDatabaseDataContext = New MyDatabaseDataContext() Dim test = context.Employe...

Linq: How to load a second table directly?

Hi, I have 2 tables here: Auction and Article. 1 Auction has 1 Article. 1 Aricle has x Auctions. Now I load a list of auctions: using (APlattformDatabaseDataContext dc = new APlattformDatabaseDataContext()) { List<Auktion> auctionlist = (from a in dc.Auktion whe...

How to write LINQ which function same as SQL like?

How to write linq with same function of following sql Like: select * from table where col like param? ...

How can I access the loop index inside a LINQ select?

This code: var customers = from cust in Customers group cust by new {cust.Country} into grouping select new { Country = grouping.Key.Country, Customers = grouping }; customers.ToList().ForEach(g => Console.WriteLine("{0} has {1} customers: {2}", g.Country, g.Customers.Count(), String.Joi...

XML to LINQ Question/s

Hello, Just before I begin heres a small overview of what I'm trying to achieve and then we'll get down to the gory details. At present I'm developing an application which will monitor a users registry for changes to specific keys which relate to user preferences. Were currently using mandatory profiles (not my choice), anyway the whole...

LINQ syntax help: projection and grouping

New to LINQ.. I am curious as to the syntax to do the following SQL query in LINQ SELECT MAX(TMPS), DAY FROM WEATHERREADINGS GROUP BY WEATHERREADINGS.DAY What I have so far: var minTemps = from ps in ww.WEATHERREADINGS group ps by ps.DATE.Hour into psByHour select new { ...

Functionally traversing a tree in C#

Consider the following extension method in c#, Traverse: IEnumerable<T> Traverse<T>( this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse ); This method allows one to recurse through a tree as defined by T and whatever function causes T to return its subnodes. Now consider the following impleme...

Custom Collection extends List<T> Add method

I want to create a custom collection and add my own custom Add method. Scenario: Teacher.Students.Add(Student s) I want to put LINQ methods to save the teacher/student relationship to the database, not just add it to the list. How can I do that? How can I know what "Teacher" object this is? ...

How to relate entities without navigation property mappings

Hi, I Have Database that contains 4 tables TABLE TBLCARTITEM (CART_ID, ITEM_ID, PROMOTION_ID, many more cart item fields) TABLE XREFCARTITEMPROMOTION (CART_ID, ITEM_ID, PROMOTION_ID) TABLE TBLPROMOTION (PROMOTION_ID, PROMOTION_TYPE_ID, many more promotion fields) TABLE LKPROMOTIONTYPE (PROMOTION_TYPE_ID, PROMOTION_TYPE_DESCRIPTION)...