Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

Can't use LINQ in my project

I am using VS 2015 and have the following object:
    public class CatholicLinksResponse
    {
        public List<CatholicLinkResponse> Links { get; set; }
    }

    public class CatholicLinkResponse
    {
        public int Id { get; set; }
        public int CatholicLinkId { get; set; }
        public string CatholicLink { get; set; }
        public string CatholicLinkName { get; set; }
    }

Open in new window


I have the following declaration:
CatholicLinksResponse catholicLinksResponse

I want to order the Links list in catholicLinksResponse by the CatholicLinkName using OrderBy by the OrderBy method is not available on the List. I have a reference to System.Data.Linq in my project.  Can someone tell me what I need to add to be able to use LINQ in my project?
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

This object catholicLinksResponse is a class and it does not implement the IEnumerable interface does not support linq. You would need to do something like this, catholicLinksResponse.Links.OrderBy(...);, the list object does implement the IEnumerable interface.

Also the links list is not initialize and therefore will be null. Try changing the definition to this.
public class CatholicLinksResponse
{
    private List<CatholicLinkResponse> theLinks;

    public CatholicLinksResponse()
    {
        theLinks = new List<CatholicLinkResponse>();
    }
    public List<CatholicLinkResponse> Links 
    {
        get { return theLinks; }
        set { theLinks = value; }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
anarki has answered the original question, you want to use System.Linq and not System.Data.Linq.

That being said, Fernando has pointed out a way to enhance your design in such a way that your CatholicLinksResponse class is actually an enumerable type or a list of CatholicLinkResponse; e.g. -
using System;
using System.Collections.Generic;
using System.Linq;

namespace EE_Q28948800
{
	class Program
	{
		static void Main(string[] args)
		{
			var responses = new CatholicLinkResponses((from i in Enumerable.Range(0, 20) select new CatholicLinkResponse() { ID = i, LinkID = i * 10, Link = string.Format("Link{0}", (char)(90 - i)), LinkName = string.Format("Link{0}", (char)(90 - i)) }));
			Console.WriteLine("Reponses:");
			foreach (var response in responses)
				Console.WriteLine(response);

			Console.WriteLine();
			Console.WriteLine("Reponses Ordered By LinkName:");
			foreach (var response in responses.OrderBy(l => l.LinkName))
				Console.WriteLine(response);

			Console.ReadLine();
		}
	}

	class CatholicLinkResponses : List<CatholicLinkResponse>
	{
		public CatholicLinkResponses() : base() { ;}
		public CatholicLinkResponses(int capacity) : base(capacity) { ;}
		public CatholicLinkResponses(IEnumerable<CatholicLinkResponse> source) : base(source) { ;}
	}

	class CatholicLinkResponse
	{
		public int ID { get; set; }
		public int LinkID { get; set; }
		public string Link { get; set; }
		public string LinkName { get; set; }

		public override string ToString()
		{
			return string.Format("ID: {0}; LinkID: {1}; Link: {2}; LinkName: {3}", ID, LinkID, Link, LinkName);
		}
	}
}

Open in new window

Produces the following output -User generated image-saige-
Avatar of dyarosh
dyarosh

ASKER

Thank you.  That is what I was missing.  Sorry it took me so long to respond.