dyarosh
asked on
Can't use LINQ in my project
I am using VS 2015 and have the following object:
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?
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; }
}
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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. -
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);
}
}
}
Produces the following output --saige-
ASKER
Thank you. That is what I was missing. Sorry it took me so long to respond.
Also the links list is not initialize and therefore will be null. Try changing the definition to this.
Open in new window