linq

Linq Queries converting to lambda expressions ?

Hello from item in range where item % 2 ==0 select i ; is converting to lamda expressions as below range.where(item % 2 ==0).select(x=>x). I feel that first way of linq is translating next one and if it is ,so is there any optimization like this range.where(item & 2 == 0) instead of other one ? ...

XDocument containing namespaces

I have the following XML which I am trying to query with XDocument: <E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent"&gt; <System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system"&gt; <EventID>589828</EventID> <Type>3</Type> <SubType Name="Information">0</SubType> ...

How do I programmatically translate a LINQ query to readable English text that correctly describes the linq expression?

I am working on a project that uses Albahari's PredicateBuilder library http://www.albahari.com/nutshell/ to create a linq expression dynamically at run time. I would like to find a way to translate this dynamically created linq predicate of type Expression<Func<T, bool>> into a readable english statement at runtime. I'll give a stati...

LINQ to SQL - Why can't you use a WHERE after an ORDER BY?

The following code: // select all orders var orders = from o in FoodOrders where o.STATUS = 1 order by o.ORDER_DATE descending select o; // if customer id is specified, only select orders from specific customer if (customerID!=null) { orders = orders.Where(o => customerID.Equals(o.CUSTOMER_ID)...

how to populate an entity you have extended in the Entity Framework?

I have an entity in my EDMX that I've extended with a few fields in a partial class like this: public partial class Employee { public string JobName {get;set;} } These properties are for display only. In the above example say the entity has a JobTypeID property. I wish JobName to be populated w/ the name that belongs to that J...

LINQ Queries And Context

Hello SO, I have a slight issue with some code I'm writing if(parameter == 1) { var linq = from a in db.table select a; } else { var linq = from a in db.table where a.id = 1 select a; } foreach(var b in linq) { ... } So basically what's going on is that the variable "linq" is different depending on the value of "parameter". When...

IEnumerable.Cast not calling cast overload

I'm not understanding something about the way .Cast works. I have an explicit (though implicit also fails) cast defined which seems to work when I use it "regularly", but not when I try to use .Cast. Why? Here is some compilable code that demonstrates my problem. public class Class1 { public string prop1 { get; set; } public ...

Dynamic Data Associate Related Table Value?

I have create a LINQ-to-SQL project in Visual Studio 2010 using Dynamic Data. In this project I have two tables. One is called phones_extension and the other phones_ten. The list of columns in phones_extension looks like this: id, extension, prefix, did_flag, len, ten_id, restriction_class_id, sfc_id, name_display, building_id, floor, ro...

linq "let" translation

I understand that when the C# compiler sees a linq query comprehension, it basically does a straight translation to the corresponding Linq Extension methods and lambdas. i.e. from x in list select x.property gets translated to: list.Select(x => x.property) my question is what do let clauses get translated to. for example how would...

My Linq to Sql Insert code seems to work fine but I don't get a record in the database

Here is my code. In the debugger, I can see that the code is running. No errors are thrown. But, when I go back to the table, no row has been inserted. What am I missing?? protected void submitButton_Click(object sender, EventArgs e) { CfdDataClassesDataContext db = new CfdDataClassesDataContext(); string sOfficeSought = office...

LINQ with Stored Procedures for XML Raw

I am new to LINQ and trying to learn it. I have a stored proc which returns XML Raw and I need to call this proc using VB.NET and LINQ... ...

Add namespaces with and without names to an XElement

Hey all I need so generate XML like the following: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt; <url> <loc>http://www.xyz.eu/&lt;/loc&gt; <lastmod>2010-01-20T10:56:47Z</lastmod> <c...

How do I select the item with the highest value using LINQ?

Imagine you got a class like this: class Foo { string key; int value; } How would you select the Foo with the highest value from an IEnumeralbe<Foo>? A basic problem is to keep the number of iterations low (i.e. at 1), but that affects readability. After all, the best I could find was something along the lines of this: IEnum...

Read Xlement fully using LinQ

Hi, I have an XElement which I am getting after parsing an xml. This XElement needs to be read only when the need arises so, I have stored it in a list for future use. I have to read this XElement using LinQ. XDocument doc = XDocument.Parse(DataManager.offeringElements[index].DataElem.ToString()); var docNode = from dataNode i...

Use LINQ to group a sequence by date with no gaps

I'm trying to select a subgroup of a list where items have contiguous dates, e.g. ID StaffID Title ActivityDate -- ------- ----------------- ------------ 1 41 Meeting with John 03/06/2010 2 41 Meeting with John 08/06/2010 3 41 Meeting Continues 09/06/2010 4 41 Meeting Continues...

Use of Distinct with list of Custom Object

How can I make the Distinct() method work with a list of custom object(Href in this case), here is what the current object looks like: public class Href : IComparable, IComparer<Href> { public Uri URL { get; set; } public UrlType URLType { get; set; } public Href(Uri url, UrlType urltype) ...

Duplicate elements when adding XElement to XDocument

I'm writing a program in C# that will go through a bunch of config.xml files and update certain elements, or add them if they don't exist. I have the portion down that updates an element if it exists with this code: XDocument xdoc = XDocument.Parse(ReadFile(_file)); XElement element = xdoc.Elements("project").Elements("logRotator") ...

DuplicateKeyException on empty table

I get a System.Data.Linq.DuplicateKeyException when adding an entity to an empty table. grade g = new grade(); g.subject = dc.subjects.Single(x => x.subjectID == 5); g.student = aStudent; dc.grades.InsertOnSubmit(g); I dropped the entire table itself and recreated it with no luck. Grades is a join table with subjectID and studentID as...

What are the similarities and differences between Groovy and LINQ?

Hi, today twitting with a colleague http://bit.ly/addZQx, he said me that Groovy is like C# but without LINQ. I don't know much about LINQ, but I answered him that Groovy has similarities to LINQ http://bit.ly/bKNrlj, for example, Groovy's class DataSet. Can you tell me more about similarities / differences between Groovy and LINQ (C#)...

Custom sort logic in OrderBy using LINQ

What would be the right way to sort a list of strings where I want items starting with an underscore '_', to be at the bottom of the list, otherwise everything is alphabetical. Right now I'm doing something like this, autoList.OrderBy(a => a.StartsWith("_") ? "ZZZZZZ"+a : a ) ...