简体   繁体   中英

How to count item in list<int>, which match with a value (true/false) in all records in another table in c# (better than with linq)

I have data as below:

图片

I also have list int list Int = new List<int>(new int[] { 1, 2, 3, 13, 102 } );

So, how to count elements in list int, what have value == "AnswerId" and have " IsCorrected == true " in picture table data by linq?

The result will be 3.

I hope I understood your question correctly and you're looking for how to query this using LINQ on something line EF. You can use the following where MyTable is the table name and IsCorrected property is a boolean.

var count = dbContext.MyTable.Count(p=> Int.Contains(p.AnswerId) && p.IsCorrected);

Assuming that you have your answers in a collection named answers and that these are plain objects:

answers.Where(a => Int.Contains(p.AnswerdId) && p.IsCorrected).Count()

Unrelated to your question, but Int is not a good name for your list of ids (matches the name of a data type and does not really convey what it's holding), a better name would be answerIds or validAnswerIds .

Refer to this link

http://www.tutorialsteacher.com/linq/linq-aggregation-operator-count

var answer = Int.Count(s => s.IsCorrected == True);

Lets say you have a data class as a model for this table

class Data
{

    public Data(int answerId, string answerContent, int? questionReadingId, int? questionListeningId, bool isCorrected)
    {
        AnswerId = answerId;
        AnswerContent = answerContent;
        QuestionReadingId = questionReadingId;
        QuestionListeningId = questionListeningId;
        IsCorrected = isCorrected;
    }

    public int AnswerId { get; set; }
    public string AnswerContent { get; set; }
    public int? QuestionReadingId { get; set; }
    public int? QuestionListeningId { get; set; }
    public bool IsCorrected { get; set; }

}

following code can get the desired output

private static List<Data> _lstData = new List<Data>();
    private static List<int> _answerIdList;
    static void Main(string[] args)
    {

        PopulateData();
        ProcessData();
    }

    private static void PopulateData()
    {
        _lstData.Add(new Data(1, "Dong", 1, null, true));
        _lstData.Add(new Data(2, "Tay", 1, null, false));
        _lstData.Add(new Data(3, "Nam", 1, null, false));
        _lstData.Add(new Data(4, "Bac", 1, null, false));
        _lstData.Add(new Data(11, "AAA", 4, null, false));
        _lstData.Add(new Data(12, "BBB", 4, null, false));
        _lstData.Add(new Data(13, "CCC", 4, null, true));
        _lstData.Add(new Data(14, "DDD", 4, null, false));
        _lstData.Add(new Data(96, "AAA", null, 1, false));
        _lstData.Add(new Data(97, "BBB", null, 1, true));    
        _lstData.Add(new Data(98, "CCC", null, 1, false));
        _lstData.Add(new Data(99, "DDD", null, 1, false));
        _lstData.Add(new Data(102, "sdasd", 24, null, true));
        _lstData.Add(new Data(103, "sadas", 24, null, false));
        _lstData.Add(new Data(104, "dasd", 24, null, false));
        _lstData.Add(new Data(105, "sadasddsds", 24, null, false));   

        _answerIdList = new List<int>(new int[] { 1, 2, 3, 13, 102 });

    }

    private static void ProcessData()
    {
        var processedDataCount = _lstData.Where(x => _answerIdList.Contains(x.AnswerId) && x.IsCorrected == true).Count();
        Console.WriteLine(processedDataCount);
    }

The ProcessData() method will print the result on the console. I have used your test data for this program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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