linq

How to build where clause in SQL Server??

I would like to build the where clase of a sql statement dynamically from hashtable in C#. The key of the hash_table will be the column's name to be inserted and the value of hash_table will be value. string sql_1="SELECT COL_1,COL_2 FROM MY_TABLE"; string sql_2="SELECT * FROM MY_TABLE WHERE COL_3='ABC'"; //note: some statment have wh...

How to write the content of a dictionary to a text file?

I have a dictionary object of type Dictionary and trying to use StreamWriter to output the entire content to a text file but failed to find the correct method from the Dictionary class. using (StreamWriter sw = new StreamWriter("myfile.txt")) { sw.WriteLine(dictionary.First()); } I can only retrieve the f...

What's wrong with this XPath?

Hi, I have this XML: <rootCategories> <category id="1"> <category id="2"> <category id="3"> <category id="4" /> <category id="5" /> <category id="6" /> </category> <category id="7" /> </category> </category> </rootCategories> A...

linq multiple order DESCENDING

.OrderBy(y => y.Year).ThenBy(m => m.Month); How to set descending order? EDIT: I tried this: var result = (from dn in db.DealNotes where dn.DealID == dealID group dn by new { month = dn.Date.Month, year = dn.Date.Year } into date orde...

How the C# compiler treats query expressions ?(Dotnet 3.5, C#3.0)

Going thru one of my favourite authors question What’s the hardest or most misunderstood aspect of LINQ? I am basically looking for the answer of the question: How the C# compiler treats query expressions Thanks ...

Linq: Converting flat structure to hierarchical

What is the easiest and somewhat efficient way to convert a flat structure: object[][] rawData = new object[][] { { "A1", "B1", "C1" }, { "A1", "B1", "C2" }, { "A2", "B2", "C3" }, { "A2", "B2", "C4" } // .. more }; into a hierarchical structure: class X { public X () { Cs = new List<string>(); } public str...

Filter a LINQ query

Here's my query. var query = from g in dc.Group join gm in dc.GroupMembers on g.ID equals gm.GroupID where gm.UserID == UserID select new { id = g.ID, name = g.Name, pools = (from pool in g.Pool // more stuff to populate pools So I have...

Need help with a LINQ ArgumentOutOfRangeException in C#

Hi there, Hoping this is a nice softball of a question for a friday but I have the following line of code: //System.ArgumentOutOfRangeException generated if there is no matching data currentAnswers = new CurrentAnswersCollection() .Where("PARTICIPANT_ID", 10000).Load()[0]; CurrentAnswersCollection is a strongly-typed collection p...

How to store a list in a column of a database table.

Howdy! So, per Mehrdad's answer to a related question, I get it that a "proper" database table column doesn't store a list. Rather, you should create another table that effectively holds the elements of said list and then link to it directly or through a junction table. However, the type of list I want to create will be composed of un...

how can i sort array via linq?

if i trying to sort my columns return 0 valu. But i need max value descinding value but how? for (int j = 0; j < dTable.Columns.Count; j++) for (int i = 0; i < dTable.Rows.Count; i++) { mycounter[i] = dTable.Rows[i][j].ToString().Length; } mycounter = mycou...

C# & ASP.Net - determine linq query generation time

I'd like to detemine the amount of time it takes for my ASP.Net program to generate certain sql queries using linq.... note - I want the query generation time, not the query execution time. Is this possible, or even feasable (if its usually fast)? My website has some heavy traffic and I want to cover all of my bases. ...

LinqProvider that gets data from from two places and merges them

Hi all, I have a Linq query that looks something like the following var query3 = from c in Session.CreateLinq<AccountTransaction>() join a in Session.CreateLinq<Account>() on c.Account equals a where c.DebitAmount >= 0 select new { a.Name, c.DebitAmount } ; The Session object interacts with a da...

LINQ to determine if children exist

For simplicity, a "Section" object contains the following properties: SectionId ParentSectionId Name I currently have the following LINQ code to obtain child sections of a given section: List<Section> sections = SectionCache.GetAllSections(); sections.AsQueryable().Where(s => s.ParentSectionId == 10); This gives me all children of...

Linq / Lambda Expressions / Delegates - What's the best explanation you've seen on this?

I'm interested in knowing where you have found the best, clearest and/or simplest explanation of the various technologies used in Linq, such as Lambda's, delegates etc. There are many books available on this subject, but I am looking for some source that goes into painstakingly great detail to make the subject as simple as possible. ...

LINQ Group By Subtotal & Total

I have a Batch with BatchItems entered by multiple users. I'm trying to not only get the subtotal per user for a single batch, but also grand total for that same batch regardless of the user grouping. Its this last part that I can't figure out. How might I get that total in order to return it as a list? from b in context.BatchItem ...

LINQ to XML - Adding a node to a .csproj file.

I've written a code generator, that generates C# files. If the file being generated is new, I need to add a reference to it to our .csproj file. I have the following method that adds a node to a .csproj file. private static void AddToProjectFile(string projectFileName, string projectFileEntry) { StreamReader streamReader = new Strea...

Bug in enumerable.range?

I have this code: public enum MyEnum { First = 6, Data1 = 6, Data2 = 7, Data3 = 8, Data4 = 9, Data5 = 10, Last = 10, Invalid = -1 }; Enumerable<int> _myTypes = Enumerable.Range((int)MyEnum.First, (int)MyEnum.Last); This creates an enumerable with elements from 6 to 15. I have equivalent code starting with 1 and it works as expected. ...

On ASP.NET ListView with LinqDataSource, display the data when the search button clicked.

Hi all, I have a ListView setup with LinqDataSource and a button that triggers search function. To avoid display data on page_load, I set ListView's DataSourceID in the Click event of the search button, bind it and set result data in LinqDataSource's Selecting event. It works as I expected but It does't look pretty to set DataSourceId i...

How to avoid Linq chaining to return null?

I have a problem with code contracts and linq. I managed to narrow the issue to the following code sample. And now I am stuck. public void SomeMethod() { var list = new List<Question>(); if (list.Take(5) == null) { } // resharper hints that condition can never be true if (list.ForPerson(12) == null) { } // resharpe...

Create a new row from 2 tables

Hello, New to LINQ and c# but having problem with one specific problem. I have 2 Data tables. DataTable 1 has Name, House Number in it. DataTable 2 holds Street, FirstHouseNumber, LastHouseNumber. What I want to do is create a new table that has Name, House Number, Street in it but I keep ending up with lots of DataTable1 Count * Da...