Linq Archives - Digital Tool Factory blog Linq Archives - Digital Tool Factory blog

The Digital Tool Factory Blog

A simple little linq values transformer

Somehow I’ve never actually used this keyword before, but as I gradually shift over to functional programming I came across the ForEach linq statement – for example

listOfObjects.ForEach(x => x.BodyText = x.BodyText.Replace(“,”,”, “));

And bam – all of the body text is replaced without having to loop through the enumerable.  Rather nice.  A simple little statement, but one that I’ve never really had the change to use before.


13
Aug 18


Written By Steve French

 

Linq projection with the extension method syntax

I’ve looked this up an embarrassing number of times – here is how you do linq projection with the extension method syntax

List<TypeClassification> typeClassifications = ctx.Classes.Select(e => new TypeClassification() {Name = e.Name, TypeClassificationID = e.ClassificationID}).ToList();

Hopefully I shall remember.  I’m not sure why I can’t remember the exact syntax.


18
Apr 14


Written By Steve French

 

How to Fix Missing records in Linq to Entities

The Problem

You are attempting to use Linq to Entities in the standard way, your linq model does not seem to have any records attached.  You have missing records in Linq to Entities.

You look in the database, and the proper record is there.  You do a

context.MyEntry.Count().ToString()

and it tells you there is one record there, but still when you attempt to actually retrieve a record, for example like

context.MyEntry.First().FirstName

you get a white screen.  What gives?

The Cause

It turns out the offending code looks like this

namespace MyProject.Models
{
	public class MyEntry
	{
		[Key]
		public int MyEntryId { get; set; }
                public string EmailAddress { get; set; }
                public string LastName { get; set; }
                public string FirstName;
	}
}

The Solution

Did you notice what was missing?  You don’t have the needed

{ get; set; }

after FirstName, so Linq To Entities will not retrieve the record for the database.  It’s always hard to see something missing, it took me about 20 minutes and lots of checking the wrong places to see the omission with this problem.


14
Feb 12


Written By Steve French

 

How to fix problems with Linq to Xml Namespaces

The Problem

You are trying to read data out of a standard xml file.  I found a great Linq to Xml tutorial here.  You get code that looks something like this

XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("~/Invoices.xml"));
List<Invoice> lst = new List<Invoice>();
lst= (from c in data.Descendants("invoice")
select new Invoice
{
ClientName = c.Element("client").Value,
InvoiceDate = Convert.ToDateTime(c.Element("date").Value),
}).ToList();

and you get nothing but object type no found errors.  Why?  You have problems with linq to xml namespaces.

The Cause:

The xml file you are trying to read is using a NameSpace!

The Solution:

Add in the namespace declaration and append it to the file in the right places, your code should look something like the below.

XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath(“~/Invoices.xml”));
XNamespace ns = XNamespace.Get(“http://www.SomeProvider.com/api”);
List lst = new List();
lst= (from c in data.Descendants(ns + “invoice”)
select new Invoice
{
ClientName = c.Element(ns + “client”).Value,
InvoiceDate = Convert.ToDateTime(c.Element(ns + “date”).Value),
}).ToList();

Do you see the “ns + ” part?  That tells C# to keep your data and the xml data apart.  And please note, you have to use it everywhere you reference a part of the xml document (I spent an hour finding that out).

That’s it!  Happy Coding.


05
Feb 12


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.