Problem in linq.

M

Mr. X.

Hello.
I would like to use linq in following code.

public class MyClssIndexer
{
public MyClss this[int myID]
{
get {
...
}
}
}

MyNewClss MyClssIndexer;
....


and in code :
var a = from MyClss c in MyNewClss ... // this is not correct code.

How can I search elements by Linq, if I, i.e have a class that can be
reached by an index :
MyNewClss[1] MyNewClss[2] , MyNewClass[3] etc...
(What is the correct syntax for linq?)

Any sample would be fine.

Thanks :)
 
A

Arne Vajhøj

I would like to use linq in following code.

public class MyClssIndexer
{
public MyClss this[int myID]
{
get {
...
}
}
}

MyNewClss MyClssIndexer;
...


and in code :
var a = from MyClss c in MyNewClss ... // this is not correct code.

How can I search elements by Linq, if I, i.e have a class that can be
reached by an index :
MyNewClss[1] MyNewClss[2] , MyNewClass[3] etc...
(What is the correct syntax for linq?)

public class MyClass
{
}

List<MyClass> lst;

lst[0], lst[1] and lst[2] will work out of the box.

Do you have a special requirement for MyClassIndexer that
List<MyClass> can not fulfill?

Arne
 
M

Mr. X.

Sorry,
I should post much more of the interface-code.

public class MyClssIndexer
{
public MyClss this[int myID]
{
get {
....
}
}

public myClss this[int myAltID, myName]
{
get
{
....
}
}

public Dictionary<string, myClss>.Enumerator GetEnumerator()
{
....
}
}

MyNewClss MyClssIndexer;



Peter Duniho said:
Mr. X. said:
[...]
and in code :
var a = from MyClss c in MyNewClss ... // this is not correct code.

How can I search elements by Linq, if I, i.e have a class that can be
reached by an index :
MyNewClss[1] MyNewClss[2] , MyNewClass[3] etc...
(What is the correct syntax for linq?)

The indexer is not relevant. Your class has to implement IEnumerable at a
minimum (better to implement IEnumerable<T>).

By the way, given your class declaration of "MyClssIndexer" and the use of
"MyNewClss" in your LINQ statement, the declaration "MyNewClss
MyClssIndexer;" makes no sense at all.

Also by the way, what is the point in leaving the "a" out of the word
"Class" when spelling your type name? You're only saving a single
character, and it's harder to type the name correctly (as your own post
shows, since even you can't get it right 100% of the time).

I'd suggest that you don't abbreviate things foolishly. :)

Pete
 
M

Mr. X.

And following code :
var b = from Dictionary<string, MyClss> t in MyNewClss
select t;

has following compilation error :
.... does not contain a definition for 'Cast' and the best extension method
overload 'System.Linq.Queryable.Cast<TResult>(System.Linq.IQueryable)' has
some invalid arguments
Instance argument: cannot convert from '...MyClssIndexer' to
'System.Linq.IQueryable'



Mr. X. said:
Sorry,
I should post much more of the interface-code.

public class MyClssIndexer
{
public MyClss this[int myID]
{
get {
...
}
}

public myClss this[int myAltID, myName]
{
get
{
...
}
}

public Dictionary<string, myClss>.Enumerator GetEnumerator()
{
...
}
}

MyNewClss MyClssIndexer;



Peter Duniho said:
Mr. X. said:
[...]
and in code :
var a = from MyClss c in MyNewClss ... // this is not correct code.

How can I search elements by Linq, if I, i.e have a class that can be
reached by an index :
MyNewClss[1] MyNewClss[2] , MyNewClass[3] etc...
(What is the correct syntax for linq?)

The indexer is not relevant. Your class has to implement IEnumerable at
a minimum (better to implement IEnumerable<T>).

By the way, given your class declaration of "MyClssIndexer" and the use
of "MyNewClss" in your LINQ statement, the declaration "MyNewClss
MyClssIndexer;" makes no sense at all.

Also by the way, what is the point in leaving the "a" out of the word
"Class" when spelling your type name? You're only saving a single
character, and it's harder to type the name correctly (as your own post
shows, since even you can't get it right 100% of the time).

I'd suggest that you don't abbreviate things foolishly. :)

Pete
 
M

Mr. X.

Ok.
Here's a code that compiled, but I don't know what to write in order to
retrieve data from that object by Linq.

public class MyClss
{
}

public class MyGenericClass
{
private class MyClssIndexer
{
private static Dictionary<string, MyClss> FTables = new
Dictionary<string, MyClss>();

private MyClss this[int myID]
{
get {
return null;
}
}

private MyClss this[int myAltID, myName]
{
get
{
return null;
}
}

private Dictionary<string, myClss>.Enumerator GetEnumerator()
{
return FTable.GetEnumerator();
}
}

public static MyClssIndexer Tables = new MyClssIndexer();
}

The above can be compiled,
but now : what should I write in the linq lines :
var b = from Dictionary<string, MyClss> d in
MyGenericClass.Tables
select d;

That's is compiled too, but what's next ?
(And is it right to write this way?
I need to reveal Tables[1] and also Tables[1, "Abc"] should work.

More "life" should be in linq lines.
What I.e should I write in the "where clause" ?
I am a newbie to the linq, and like more examples, that I can accomplish.

Thanks :)
 
M

Mr. X.

The code again.
(Sorry, there is no "undo" to post).

public class MyClss
{
}

public class MyGenericClass
{
public class MyClssIndexer
{
public static Dictionary<string, MyClss> FTables = new
Dictionary<string, MyClss>();

public MyClss this[int myID]
{
get {
return null;
}
}

public MyClss this[int myAltID, string myName]
{
get
{
return null;
}
}

public Dictionary<string, MyClss>.Enumerator GetEnumerator()
{
return FTables.GetEnumerator();
}
}

public static MyClssIndexer Tables = new MyClssIndexer();
}

And what should I write in the linq lines?
 
M

Mr. X.

I don't understand.
I agree that first I didn't include any real code,
but despite the old posts, last post is indeed a real code, that actually
compiled, as you have asked me to.
I know what exactly should I change in the code.

I have changed the first line to :
public class MyGenericClass: IEnumerable
but I got the line :
'MyGenericClass' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()
I don't understand because as you see in my code there is the line :
public Dictionary<string, MyClss>.Enumerator GetEnumerator()
(Anyway, isn't any way to show automatically all the methods I should
inherits from the interface?)

Please lead me to the fixed code (or the lines that I should change).

Thanks :)


Peter Duniho said:
Mr. X. said:
Ok.
Here's a code that compiled, but I don't know what to write in order to
retrieve data from that object by Linq. [...]

I already explained: your type has to implement IEnumerable or
IEnumerable<T>. Just writing a GetEnumerator() method isn't sufficient.
Your class has to actually be declared as implementing the interface.

Based on the code you've posted (which frankly is still not a complete
example), it seems to me that you ought to just return the dictionary
itself (perhaps typed only as IEnumerable<KeyValuePair<string, MyClss>>,
to make it harder for the caller to do things to the private data
structure) and forget about your own class supporting LINQ directly.

But if you insist, you need to read what I've already written (and Arne
too).

Pete
 
M

Mr. X.

Sorry,
changed :
public class MyClss
{
public int a;
public int b;
}

public class MyGenericClass
{
public class MyClssIndexer : IEnumerable
{
public static Dictionary<string, MyClss> FTables = new
Dictionary<string, MyClss>();

public MyClss this[int myID]
{
get {
return null;
}
}

public MyClss this[int myAltID, string myName]
{
get
{
return null;
}
}

// public Dictionary<string, MyClss>.Enumerator GetEnumerator()
// public IEnumerable<KeyValuePair<string, MyClss>> GetEnumerator()
public IEnumerable GetEnumerator()
{
return FTables.GetEnumerator();
}

}

public static MyClssIndexer Tables = new MyClssIndexer();
}

but still problem :
'MyGenericClass.MyClssIndexer' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()'.
'MyGenericClass.MyClssIndexer.GetEnumerator()' cannot implement
'System.Collections.IEnumerable.GetEnumerator()' because it does not have
the matching return type of 'System.Collections.IEnumerator'.

Mr. X. said:
I don't understand.
I agree that first I didn't include any real code,
but despite the old posts, last post is indeed a real code, that actually
compiled, as you have asked me to.
I know what exactly should I change in the code.

I have changed the first line to :
public class MyGenericClass: IEnumerable
but I got the line :
'MyGenericClass' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()
I don't understand because as you see in my code there is the line :
public Dictionary<string, MyClss>.Enumerator GetEnumerator()
(Anyway, isn't any way to show automatically all the methods I should
inherits from the interface?)

Please lead me to the fixed code (or the lines that I should change).

Thanks :)


Peter Duniho said:
Mr. X. said:
Ok.
Here's a code that compiled, but I don't know what to write in order to
retrieve data from that object by Linq. [...]

I already explained: your type has to implement IEnumerable or
IEnumerable<T>. Just writing a GetEnumerator() method isn't sufficient.
Your class has to actually be declared as implementing the interface.

Based on the code you've posted (which frankly is still not a complete
example), it seems to me that you ought to just return the dictionary
itself (perhaps typed only as IEnumerable<KeyValuePair<string, MyClss>>,
to make it harder for the caller to do things to the private data
structure) and forget about your own class supporting LINQ directly.

But if you insist, you need to read what I've already written (and Arne
too).

Pete
 
M

Mr. X.

Still I didn't understand.
I gave the code.
Can you send the fixed code, please?
(It just much better understood, and I should learn faster from that).

(For right click - I have VS 2008, C#, but didn't see something special,
when right click on IEnumerable)

Thanks :)

Peter Duniho said:
Mr. X. said:
[...]
I have changed the first line to :
public class MyGenericClass: IEnumerable
but I got the line :
'MyGenericClass' does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()
I don't understand because as you see in my code there is the line :
public Dictionary<string, MyClss>.Enumerator GetEnumerator()

That line doesn't implement the method the compiler is telling you that
you need to implement.
(Anyway, isn't any way to show automatically all the methods I should
inherits from the interface?)

Yes, you can right-click on the interface name and a menu should come up
offering to add the necessary members to implement the interface.

Pete
 
M

Mr. X.

O.K.
I think problem is solved (I didn't check out the code on runtime yet).
Little thing - I need one public GetEnumerator and one not public, as the
following code.
Also - When Right click on interface I should choose implement interface ->
implement interface (with no extra code exists for the interface) - for
automatic code creation.

Correct code should be :
public class MyClss
{
public int a;
public int b;
}

public class MyGenericClass
{
public class MyClssIndexer : IEnumerable<KeyValuePair<string,
MyClss>>
{
public static Dictionary<string, MyClss> FTables = new
Dictionary<string, MyClss>();

public MyClss this[int myID]
{
get {
return null;
}
}

public MyClss this[int myAltID, string myName]
{
get
{
return null;
}
}


#region IEnumerable<KeyValuePair<string,MyClss>> Members

public IEnumerator<KeyValuePair<string, MyClss>> GetEnumerator()
{
return FTables.GetEnumerator();
}

#endregion

#region IEnumerable Members

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

#endregion
}

public static MyClssIndexer Tables = new MyClssIndexer();
}

Anyway, Thanks :)

Peter Duniho said:
Mr. X. said:
[...]
public class MyClssIndexer : IEnumerable

Try:

public class MyClssIndexer : IEnumerable<KeyValuePair<string, MyClss>>

And, as you had before:

public Dictionary<string, MyClss>.Enumerator GetEnumerator()

…for the implementation.

You will also need:

private System.Collections.IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

Whether this is really the best way to do what you want to do, I can't
say. The question is too vague. But it ought to at least compile.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top