比较两个列值以及匹配和不匹配记录的计数 [英] Compare two column values and count of the matched and unmatched records
本文介绍了比较两个列值以及匹配和不匹配记录的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表格(记录),如
Id Data1 Data2
1 AB
2 CC
3 DD
4 BA
5 CD
6 AA
7 DD
我需要匹配的行数(匹配= 4& ;无与伦比= 3)使用实体框架
我完成了如下代码。我得到了我期望的结果,我需要避免性能以及代码减少。
int correctAnswersCount = 0 ;
int incorrectAnswersCount = 0 ;
int totalUnAttemptsCount = 0 ;
string [] aa =( from a in db.ALBulkTestResults 其中 a.CandidateID == candidateId 选择 a.Answer).ToArray ();
string [] bb =(来自 a in db.ALBulkTestResults 其中 a.CandidateID == candidateId select a.CandidateAns)。ToArray ();
for ( int i = 0 ; i< aa.Length; i ++)
{
string comp1 = aa [i];
string comp2 = bb [i];
if (comp1 == comp2)
{
ViewData [ correctAnswersCount] = correctAnswersCount ++;
}
else
{
if (comp2 == N)
{
ViewData [ totalUnAttemptsCount] = totalUnAttemptsCount ++;
}
else
{
ViewData [ incorrectAnswersCount] = incorrectAnswersCount ++;
}
}
}
解决方案
是的,你可以实现同样的目标通过编写非常简单的LINQ查询。
代码示例如下:
namespace ConsoleApp
{
class Program
{
static void Main(string [] args)
{
Customer cust = new Customer();
列表<客户> custList = cust.GetCustomerData();
int matchedCount = custList.Where(p => p.Data1 == p.Data2).Count();
int unmatchedCount = custList.Where(p => p.Data1!= p.Data2).Count();
}
}
公共类客户
{
public int Id {get;组; }
public string Data1 {get;组; }
public string Data2 {get;组; }
public List< Customer> GetCustomerData()
{
返回新列表< Customer> {
new Customer(){Id = 1,Data1 =A,Data2 =B},
new Customer(){Id = 2,Data1 =C,Data2 =C },
新客户(){Id = 3,Data1 =D,Data2 =D},
新客户(){Id = 1,Data1 =B,Data2 = A},
新客户(){Id = 1,Data1 =C,Data2 =D},
新客户(){Id = 1,Data1 =A, Data2 =A},
new Customer(){Id = 1,Data1 =A,Data2 =B},
new Customer(){Id = 1,Data1 =D ,Data2 =D}
};
}
}
}
你正在做很多额外的工作。
主要是减少一次通过数据。
另外,在分析结束前不要更新ViewData
。 br />
int correctAnswersCount = 0 ;
int incorrectAnswersCount = 0 ;
int totalUnAttemptsCount = 0 ;
const string NoAttempt = N;
foreach ( var a in db.ALBulkTestResults)
{
string candidateAnswer = a.CandidateAns;
if (candidateAnswer == a.Answer)
{
++ correctAnswersCount;
}
else if (candidateAnswer == NoAttempt)
{
++ totalUnAttemptsCount;
}
else
{
++ incorrectAnswersCount;
}
}
ViewData [ correctAnswersCount] = correctAnswersCount ;
ViewData [ totalUnAttemptsCount] = totalUnAttemptsCount;
ViewData [ incorrectAnswersCount] = incorrectAnswersCount;
I have table(Records) like an
Id Data1 Data2
1 A B
2 C C
3 D D
4 B A
5 C D
6 A A
7 D D
I need the Count of matched rows like (matched = 4 & unmatched = 3) using Entity Framework
and i have done the code like below. and i got what i expected result and i need to avoid the performance as well code reducing.
int correctAnswersCount = 0;
int incorrectAnswersCount = 0;
int totalUnAttemptsCount = 0;
string[] aa = (from a in db.ALBulkTestResults where a.CandidateID == candidateId select a.Answer).ToArray();
string[] bb = (from a in db.ALBulkTestResults where a.CandidateID == candidateId select a.CandidateAns).ToArray();
for (int i=0; i<aa.Length; i++ )
{
string comp1 = aa[i];
string comp2 = bb[i];
if(comp1 == comp2)
{
ViewData["correctAnswersCount"] = correctAnswersCount++;
}
else
{
if (comp2 == "N")
{
ViewData["totalUnAttemptsCount"] = totalUnAttemptsCount++;
}
else
{
ViewData["incorrectAnswersCount"] = incorrectAnswersCount++;
}
}
}
解决方案
Yes, you can achieve the same by writing very simple LINQ query.
Code sample is given below:
namespace ConsoleApp { class Program { static void Main(string[] args) { Customer cust = new Customer(); List<Customer> custList = cust.GetCustomerData(); int matchedCount = custList.Where(p => p.Data1 == p.Data2).Count(); int unmatchedCount = custList.Where(p => p.Data1 != p.Data2).Count(); } } public class Customer { public int Id { get; set; } public string Data1 { get; set; } public string Data2 { get; set; } public List<Customer> GetCustomerData() { return new List<Customer> { new Customer () { Id = 1, Data1 = "A",Data2 = "B"}, new Customer () { Id = 2, Data1 = "C",Data2 = "C"}, new Customer () { Id = 3, Data1 = "D",Data2 = "D"}, new Customer () { Id = 1, Data1 = "B",Data2 = "A"}, new Customer () { Id = 1, Data1 = "C",Data2 = "D"}, new Customer () { Id = 1, Data1 = "A",Data2 = "A"}, new Customer () { Id = 1, Data1 = "A",Data2 = "B"}, new Customer () { Id = 1, Data1 = "D",Data2 = "D"} }; } } }
You are doing a lot of extra work.
The main thing is to reduce to a single pass through the data.
Also, don't updateViewData
until the end of the analysis.
int correctAnswersCount = 0; int incorrectAnswersCount = 0; int totalUnAttemptsCount = 0; const string NoAttempt = "N"; foreach (var a in db.ALBulkTestResults) { string candidateAnswer = a.CandidateAns; if (candidateAnswer == a.Answer) { ++correctAnswersCount; } else if (candidateAnswer == NoAttempt) { ++totalUnAttemptsCount; } else { ++incorrectAnswersCount; } } ViewData["correctAnswersCount"] = correctAnswersCount; ViewData["totalUnAttemptsCount"] = totalUnAttemptsCount; ViewData["incorrectAnswersCount"] = incorrectAnswersCount;
这篇关于比较两个列值以及匹配和不匹配记录的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文