1

I have a Chat class. In chat class the is a ChatMessage List, and Inside the ChatMessage class there is a ChatHistory Class. I requirement is to get the ChatHistory Collection. When I try like below mentioned code it shows, "Cannot convert lambda expression to intended delegate type" Please give any suggestion to fix this, I have referred many answers but it is not meet my requirement

foreach (var textMessage in store.Chat.Where(s => s.ChatMessage.Where(a => a.ChatHistory.Count > 0 && a.IsDeleted == false)).ToList())

Chat.cs

List<ChatMessage> ChatMessage = new List<ChatMessage>();

ChatMessage.cs

List<ChatHistory> ChatHistory = new List<ChatHistory>();

ChatHistory.cs

public string ID { get; set; }
9
  • 3
    try this var textMessage in store.Chat.Where(s => s.ChatMessage.Any(a => a.ChatHistory.Count > 0 && a.IsDeleted == false)).ToList() Commented Oct 5, 2018 at 9:33
  • Are you operating on eg. EntityFramework? or pure lists? Commented Oct 5, 2018 at 9:33
  • @ershoaib, When I try your code, but ChatHistory Inside property is not accessed inside the foreach loop. I want to access the ChatHistory property like this, textMessage.ChatHistory.Where(a => a.ID) Commented Oct 5, 2018 at 9:39
  • @x39, I am using EntityFramework Commented Oct 5, 2018 at 9:40
  • 1
    try this => foreach (var textMessage in store.Chat.Where(s => s.ChatMessage.Any(a => a.ChatHistory.Count > 0 && a.IsDeleted == false)).SelectMany(x => x.ChatMessage).ToList()) { var a = textMessage.ChatHistory.Where(b => b.ID == ""); } Commented Oct 5, 2018 at 9:51

2 Answers 2

1

First of all here its hard to (or can't) use Where inside Where on single variable instead of this you can use Any inside Where it may gives you your output.

And by using SelectMany you can get all your ChatHistory.

foreach (var textMessage in store.Chat.Where(s => s.ChatMessage.Any(a => a.ChatHistory.Count > 0 && a.IsDeleted == false)).SelectMany(x => x.ChatMessage).ToList())
{
    var chatHistory = textMessage.ChatHistory.Where(b => b.ID == "SomeId");
}
Sign up to request clarification or add additional context in comments.

2 Comments

"First of all you can't use Where inside Where". Not quite true, you just need to use a different variable inside the nested where clause. e.g. List<List<int>> a = new List<List<int>>{ new List<int>{ 1, 2, 3 },new List<int>{ 1, 1, 0 },new List<int>{4,6}}; var b = a.Where(i=>i.Count>2 && i.Where(j=>j>2).Any());
@CooncilWorker, yes you are right but cant use where on same variable. need a different variable btw I don't mean to say you can't use where inside where all the time but its grammatical and typing mistake from me, now answer updated
0

This code:

s.ChatMessage.Where(a => a.ChatHistory.Count > 0 && a.IsDeleted == false)

will return an IEnumerable but we need a bool.

You probably want to use .Any() instead of .Where()

foreach (
    var textMessage in store.Chat.Where(
        s => s.ChatMessage.Any(
            a => a.ChatHistory.Count > 0 && a.IsDeleted == false
        )
    ).ToList()
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.