0

I have a collection FileTypes that holds objects of type FileType. That type has field CounterpartyId. I also have a collection Counterparties of objects of type Counterparty, that has field Id. Could you please help me filter out all Counterparties whose Id is not equal to any FileType's CounterpartyId, using LINQ. Thanks

David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • 1
    Three questions: Where's the code? Also, what have you tried? Finally - have you read the [Basic LINQ Query Operations](http://msdn.microsoft.com/en-us/library/bb397927.aspx) topic on MSDN? – Andras Zoltan Jun 25 '12 at 14:57
  • 1
    I am sorry, but what code? Declaration of two collections? What have I tried? I just don't know how to approach the problem. Should I shoot in darkness? I have read, but I have not enough experience with linq. – David Shochet Jun 25 '12 at 15:02
  • Rather than *describe* your collections and types in English, it helps us and future visitors if you write out the minimum detail of each class as code; and then show us the declarations of your collections. That way when we post answers we can be sure that the type names, members names etc are all relevant. It helps future visitors also then see if your question is relevant to their own problem. I ask 'what have you tried' becasue you've used the `linq` tag, so you obviously have an idea of how to achieve what you want but not showed us what you tried. As it is, Frederic has probably got it – Andras Zoltan Jun 25 '12 at 15:09
  • @casperOne I wonder why the moderator says that the question "cannot be reasonably answered in its current form", in spite of the fact that it has been reasonably answered. I also wonder what in the question was ambiguous or incomplete, or overly broad. Yes, I used plain words to describe the problem, but I doubt anybody might understand it in different ways. My question was actually oversimplified rather than overly broad. Anyway, the question was answered, it was answered well, and that proves that information I provided was enough for a person that wanted to help. – David Shochet Jun 27 '12 at 20:21
  • I don't know why one would want to see "details of each class", if I am concerned with only one field, and anything else in the class would be irrelevant to the question. Any code would make the question look more complicated than it looks now. – David Shochet Jun 27 '12 at 20:26
  • I didn't say that, the close notice did. That said, the question fails to answer the fundamental question of "what have you tried", which is the equivalent of "gimme teh codez", which we close as NARQ on Stack Overflow. – casperOne Jun 27 '12 at 20:48
  • 1
    @davidShochet I was the one that originally flagged for moderator (@casperone in this case) attention, after asking for more detail, because there is no evidence here of you trying anything and SO generally is not a code-writing service. That said, others in the community have given you an answer, that you have accepted - so you have still got what you needed. As an example please see this other question I nearly flagged today, until in the end it was edited following some guidance http://stackoverflow.com/questions/11226112/how-to-make-this-sql-in-linq-to-sql – Andras Zoltan Jun 27 '12 at 21:00
  • 1
    @davidShochet class detail is helpful to make answers more relevant and to help potential answerers with a good starting point. I've had questions closed and deleted and it stings, for sure, but it's not personal and generally happens when some basic guidelines aren't met. Do those guidelines always get followed in all cases? Absolutely not. With over 3 million questions on the site, and thousands getting added every day, its not possible to give all of them the same treatment. Anyway your question could still get reopened, that's the magic of the SO community :-) – Andras Zoltan Jun 27 '12 at 21:07
  • @davidShochet as a final word from me at least, some will say that you have been penalised for nought more than not knowing what to do. I have answered similar questions before, trying to help people who were similarly lost and not tried anything. Those people benefitted but those questions also ended up closed. There can be a discrepancy here if the subject is deemed 'easy' by many, or difficult (cryptography is common, very few know enough to be expert) - the former are often closed, the latter are not. In your case it might be harsh, I don't know, but given time the community will decide. – Andras Zoltan Jun 27 '12 at 21:17
  • @Andras Zoltan I have no problem with closing the question, as I received what I needed. I only think that what was written in the moderator's explanation was not true and thus insulting. The question was not ambiguos, vague, incomplete or overly broad. Maybe for somebody it is better to see code even if it makes things less clear than carefully chosen words. It is possible. But it still doesn't make what was said about my question true. – David Shochet Jun 28 '12 at 01:13
  • You said that I had not tried anything. I have already answered this. I didn't know how to deal with such a problem because of lack of experience with linq. Would you suggest trying any possible combination of words? Of course, I could take a class on linq or reading a big book, but I needed a solution sooner than that. I didn't demand from anybody to code for me. I only humbly and politely asked for help. If one believes my question doesn't deserve an answer, it is OK. Is not necessary to falsely accuse me against what is evident. – David Shochet Jun 28 '12 at 01:18
  • @DavidShochet if it's the wording of the notice that's at issue, it's a generic message - the reason it was closed was because of the reasons I have put in these messages. 'not a real question' is the generic banner under which such questions are closed. There are only a small number of close-reasons and in this case, as casperOne said, 'not a real question' is the one that most closely relates to those that apply here. Anyway - please don't think that I will personally do this for every question you ask - it's a judgment call and people are entitled to disagree and even reverse that call. – Andras Zoltan Jun 28 '12 at 06:04
  • @AndrasZoltan It is hard to admit being wrong, isn't it? You know very well that the notice contained more than "not a real question", but also "This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form", which all is not true. You say there is nothing that fits the question better, but I would say it is hard to make up something farther from the truth. And if there is really nothing better than lie, why not just leave the question alone, rather than hurt somebody's feelings? – David Shochet Jun 28 '12 at 11:13
  • @DavidShochet It's not personal, and if I were you I wouldn't accuse someone of not being able to 'admit they're wrong' without considering how many comments they might have retracted, or answers they've deleted following comments, or questions they've deleted following 'peer pressure' etc etc in the past. If your feelings have been hurt, then I apologise for my part in that but, ultimately, asking for you to improve your question based on the guidelines of the site is a perfectly reasonable thing to do - and if you choose to ignore that request, you have to be able to accept getting closed. – Andras Zoltan Jun 28 '12 at 11:40
  • @Andras Zoltan It is not about getting closed, it is about false statements about the question. – David Shochet Jun 28 '12 at 12:56
  • @davidShochet then I suggest you head over to http://meta.stackoverflow.com and complain about these standard, non-editable notices that the site authors have coded in. – Andras Zoltan Jun 28 '12 at 18:10

3 Answers3

9

You could use Any() and write something like:

var filteredCounterparties = counterparties.Where(
    cp => !fileTypes.Any(ft => cp.Id == ft.CounterpartyId));
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479
2
IENumerable<Counterparties> filteredcounterparties = Counterparties.Where(w => w.Id != fileType.CounterPartyId);
Liam
  • 27,717
  • 28
  • 128
  • 190
2

I would try something like this.

   var filteredcounterparties = counterparties.Where(x => x.Id != fileType.CounterPartyId);

That should do the trick

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47