简体   繁体   English

针对成员对象列表过滤LINQ查询

[英]Filtering LINQ Query against member object list

I have an application that I inherited from a coworker that tracks feedback cards. 我有一个应用程序,我继承自跟踪反馈卡的同事。 I also have a form that filters the cards that are displayed on a web page based upon a number of user entered filters. 我还有一个表单,可以根据用户输入的多个过滤器过滤网页上显示的卡片。 All of the filters work fine, except the filter that is applied against feedback details (service was fine/bad, room was clean/dirty, etc). 所有过滤器都可以正常工作,除了针对反馈细节应用的过滤器(服务很好/坏,房间干净/脏,等等)。 These are stored in list of a member class in my card class. 这些存储在我的卡类中的成员类列表中。

Below is a set of snippets of each class. 下面是每个类的一组片段。

public class Card {
    public long ID { get; set; }
    public List<Feedback> feedback { get; set; }

    ...
}

public class Feedback {
    public long ID {get; set; }

    ...
}

public class CardFilter {
    public ICollection<long> FeedBackDetails {get; set; }

    ...
}

...
public IQueryable<CardType > GetFeedbackQueryable<CardType>(CardFilter filter = null)
        where CardType : Card
{
    var data = Service.GetRepository<CardType>();
    var CardQuery = data.All;
    ...
    if (filter.FeedbackDetails != null && filter.FeedbackDetails.Count != 0)
    {
        cardQuery = cardQuery.Where(card => card.FeedbackValues)
                             .All(fbv => filter.FeedbackDetails.Contains(fbv.ID));
    }
    return cardQuery;
}
...

When I try the filter: 当我尝试过滤器时:

cardQuery = cardQuery.Where(card => card.FeedbackValues)
                     .All(fbv => filter.FeedbackDetails.Contains(fbv.ID));

It returns the 15 card instances without any feedback. 它返回15个卡实例,没有任何反馈。 If I use the filter: 如果我使用过滤器:

cardQuery = cardQuery.Where(card => card.FeedbackValues)
                     .Any(fbv => filter.FeedbackDetails.Contains(fbv.ID));

Nothing is returned, even though I can look through the data and see the appropriate cards. 即使我可以查看数据并查看相应的卡片,也不会返回任何内容。

I'm new to LINQ, so I know I'm missing something. 我是LINQ的新手,所以我知道我错过了一些东西。 Please point me in the right direction here. 请指出我在正确的方向。

EDIT: 编辑:

To give a little more background on this application, I'll be a bit more verbose. 为了给出这个应用程序的更多背景知识,我会更加冗长。 The Card table/Model has the information about the card and the person submitting it. 卡表/模型包含有关卡和提交卡的人的信息。 By that I mean name or anonymous, address, location being commented upon and a few other basic facts. 我的意思是姓名或匿名,地址,评论的位置和一些其他基本事实。 The feedback items are listed in another table and displayed on the web form and the user can check either positive or negative for each. 反馈项列在另一个表中并显示在Web表单上,用户可以检查每个表的正面或负面。 There are three possible answers for each feedback detail; 每个反馈细节有三种可能的答案; 0 (positive), 1 (negative) or nothing (no answer). 0(正),1(负)或无(无答案)。

The Card Model has all of the basic card information as well as a collection of feedback responses. 卡模型具有所有基本卡信息以及一组反馈响应。 My filter that is giving me trouble is against that collection of responses. 我给你带来麻烦的过滤器反对那些回复。 Each card can have from 0 to 52 possible responses which may not apply to all situations, so I need to see all cards that are about a specific situation (cleanliness, etc.) whether they are positive or negative. 每张卡片可能有0到52种可能的响应,这可能不适用于所有情况,所以我需要查看所有关于特定情况(清洁度等)的卡片,无论它们是正面还是负面。 That is the purpose of this filter. 这就是这个过滤器的目的。

var ifExist = YourList.Any(lambda expression) checking if YourList<T> contains object whitch fulifill lambda expression . var ifExist = YourList.Any(lambda expression)检查YourList<T>包含对象whif fulifill lambda表达式。 It's only return true or false . 它只会返回truefalse If you want to have list of objects you should use var YourNewList = YourList.Where(lambda expression).ToList() . 如果你想拥有对象列表,你应该使用var YourNewList = YourList.Where(lambda expression).ToList()

Try this. 试试这个。 Although I'm not entirely sure about your filter obj. 虽然我不完全确定你的过滤器obj。

cardQuery = cardQuery.Query().Select(card => card.FeedbackValues).Where(fbv => filter.FeedbackDetails.Contains(fbv.ID));

You can't use the all statement, the predicate for this statement is if all values are identical to the id. 您不能使用all语句,如果所有值都与id相同,则此语句的谓词。

In your where statement, which is a filter clause, are you not filtering any thing. 在你的where语句中,这是一个过滤子句,你不过滤任何东西。

And you are comparing feedbackvalues with an id? 你正在将反馈值与id进行比较? Are they the same? 它们是一样的吗? Can you post some more details about 你能发布更多细节吗?

Maybe try: 也许试试:

cardQuery = cardQuery.Where(card => filter. FeedbackDetails.Contains(card. Id/detsils))
                     .Select(se=> se).Tolist() ;

I was able to solve this. 我能够解决这个问题。 All of the answers that were posted helped me in the right direction. 发布的所有答案都帮助我朝着正确的方向前进。 I wish I could have flagged them all as the answer. 我希望我能把所有人都标记为答案。

I ended up reworking my Feedback model slightly to include another identity field from the database. 我最后重新修改了我的反馈模型,以包含数据库中的另一个身份字段。 It duplicated existing date (bad design, I know. It wasn't mine), but had a unique name. 它重复了现有的日期(糟糕的设计,我知道。这不是我的),但有一个独特的名称。 Using the new field, I was able to apply an Any filter. 使用新字段,我可以应用Any过滤器。 I guess I was confusing LINQ with multiple fields named ID. 我想我将LINQ与名为ID的多个字段混淆。 Once I used FeedbackID, it worked fine. 一旦我使用FeedbackID,它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM