brother
brother

Reputation: 8171

Simple LINQ statement failing - why?

I have the following 2 lines;

var currentPage = Directory.GetFiles(@"D:\mydir", "*.pdf")
  .Skip((pageNum - 1) * pageSize).Take(pageSize);

var allItems = currentPage.OrderBy(c => c)
  .Select(c => new ListViewItem(c)).ToArray();

The above 2 line is failing with "CS1502: The best overloaded method match for 'System.Web.UI.WebControls.ListViewItem.ListViewItem(System.Web.UI.WebControls.ListViewItemType)' has some invalid arguments". The error is, according to VS2010, from "new ListViewItem(c)".

I am thinking I am missing something really obivous - but I just cannot see it.

Upvotes: 0

Views: 196

Answers (5)

code4life
code4life

Reputation: 15794

So it looks like you're trying to set bind the ListView to this array of specified file names. Have you tried setting the DataSource of the ListView?

Like this:

myListView.DataSource = null;

var currentPage = Directory.GetFiles(@"D:\mydir", "*.pdf")
  .Skip((pageNum - 1) * pageSize).Take(pageSize);

var dataArray = currentPage.OrderBy(c => c).ToArray();

myListView.DataSource = dataArray;
myListView.DataBind();

More on binding to an IEnumerable : http://forums.asp.net/t/1319474.aspx/1

Hope that helps...

Upvotes: 0

jacqijvv
jacqijvv

Reputation: 880

var allItems = currentPage.OrderBy(c => c).Select(c => new ListViewItem((ListViewItemType)Enum.Parse(typeof(ListViewItemType), c, true))).ToArray();

Gets rid of the error, dont know if that will help you

Upvotes: 0

TehBoyan
TehBoyan

Reputation: 6890

You are trying to pass the ListViewItem a result from the previous query that is a string(of the filename). The ListViewItem does not have a constructor that accepts that.

Try reading the strings in a List or something and then add it as a datasource to the ListView control.

Upvotes: 0

DaveShaw
DaveShaw

Reputation: 52798

If this is you should Remove the using System.Web.UI.WebControls; directive and ensure you have using System.Windows.Forms; instead.

If this is you can't create an instance of ListViewItem with a String as the parameter for the constructor. See here on MSDN for more info on the constructor.

Upvotes: 0

Conrad Frix
Conrad Frix

Reputation: 52675

Directory.GetFiles() returns an array of strings. You are then taking those strings and trying to create an array of System.Web.UI.WebControls.ListViewItem by passing in a string into the ListViewItem constructor.

The problem is there is no constructor of ListViewItem that takes in a string.

The only constructor takes in a ListViewItemType

public ListViewItem(
    ListViewItemType itemType
)

This is why you got the error

The best overloaded method match for System.Web.UI.WebControls.ListViewItem.ListViewItem(System.Web.UI.WebControls.ListViewItemType) has some invalid arguments

Upvotes: 2

Related Questions