Related
In EF core I have a list of composite Id's, and I then want to have of those ids from the database.
var crits = new List<MyCrit>()
{
new MyCrit() {Key1 = "A", Key2 = 3},
new MyCrit() {Key1 = "B", Key2 = 4}
};
it should end up with SQL like this:
select * from MyTable where (Key1="A" and Key2 = 3) or (Key1="B" and Key2 = 4)
I have configured the table to have correct setup, but I cannot get the OR.
Here is my code:
var query = _db.MyTable.AsQueryable();
foreach (var crit in crits)
{
query = query.Where(m => m.Key1 == crit.Key1 && m.Key2 == crit.Key2);
}
Unfortunatly that gives me this SQL:
select * from MyTable where (Key1="A" and Key2 = 3) and (Key1="B" and Key2 = 4)
I can't figure out how to add inside the loop so it becomes OR.
Use FilterByItems extension and you can simplify your query to the following:
var query = _db.MyTable
.FilterByItems(crits, (m, crit) => m.Key1 == crit.Key1 && m.Key2 == crit.Key2, true);
I have some hope this should work - just Concat the queries together. It should only result in a single query against the database.
using System.Collections.Generic;
using System.Linq;
public class Program
{
public static void Main()
{
var crits = new List<MyCrit>()
{
new MyCrit() {Key1 = "A", Key2 = 3},
new MyCrit() {Key1 = "A", Key2 = 4}
};
// _db.Table.AsQueryable()
var table = Enumerable.Empty<DbValue>().AsQueryable();
// Could use a foreach over crits with an initial value of Enumerable.Empty<DbValue>().AsQueryable()
var filtered = crits.Select(x => table.Where(y => y.Key1 == x.Key1 && y.Key2 == x.Key2))
.Aggregate(Enumerable.Empty<DbValue>().AsQueryable(), (x, y) => x.Concat(y));
}
}
public class MyCrit
{
public string Key1;
public int Key2;
}
public class DbValue
{
public string Key1;
public int Key2;
public string OtherData;
}
Maybe something like this? (I wrote it from memory and fast so something can work wrong...)
UPDATE
Now I could check it and fix and now should work
Expression CreateBasicExpression(ParameterExpression parameterExpression1, string crit)
{
var rightPart = Expression.Equal(Expression.Property(parameterExpression1, "Name"), Expression.Constant(crit));
var leftPart = Expression.Equal(Expression.Property(parameterExpression1, "Surname"), Expression.Constant(crit));
return Expression.And(leftPart, rightPart);
}
var parameterExpression = Expression.Parameter(typeof(MyTable));
Expression basicExpression = null;
var crits = new List<string>
{
"test1",
"test2"
};
foreach (var crit in crits)
{
if (basicExpression is null)
{
basicExpression = CreateBasicExpression(parameterExpression, crit);
}
else
{
basicExpression = Expression.Or(basicExpression, CreateBasicExpression(parameterExpression, crit));
}
}
var resultExpression = Expression.Lambda(basicExpression, parameterExpression);
var castedExpression = (Expression<Func<MyTable, bool>>)resultExpression
I am trying to convert below SQL to LINQ query and I am stuck with filtering results using the Match in Left outer Join.
SELECT #batchID , IQ1.ID, #environment , 'IRD', IQ1.ReportingDate, IQ1.Match
FROM (
SELECT TD.*, RD.Match
FROM TransactionDetail TD
INNER JOIN .dbo.Measure M ON M.InternalID = TD.MetricCode-- and TD.BatchID = 'e07f9855-b286-4406-9189-5cfb2a7914c8'
LEFT OUTER JOIN (
SELECT tmp.ID, tmp.ReportingDate, 1 AS Match
FROM tmp
) AS RD ON RD.ID = M.Frequency AND RD.ReportingDate = TD.ReportingDate
WHERE RD.Match IS NULL AND
TD.BatchID = #batchID AND
NOT EXISTS (SELECT TransactionFailureReasonID FROM TransactionDetailFailureReasons R WHERE R.TransactionDetailID = TD.ID and R.TransactionFailureReasonID = 'NRD') AND
NOT EXISTS (SELECT TransactionFailureReasonID FROM TransactionDetailFailureReasons R WHERE R.TransactionDetailID = TD.ID and R.TransactionFailureReasonID = 'RDP') AND
NOT EXISTS (SELECT TransactionFailureReasonID FROM TransactionDetailFailureReasons R WHERE R.TransactionDetailID = TD.ID and R.TransactionFailureReasonID = 'RDF')
) AS IQ1
I have so far achieved below,
// Prepare data for left outer join
var rd = (from tt in result
select new { ID = tt.Id, tt.ReportingDate });
// inner join
var td = TransactionDetail.Join(
MesureTb,
t => t.MetricCode,
m => m.InternalId,
(t, m) => new
{
t.Id,
t.RowAction,
t.BatchId,
t.TrustCode,
t.MetricCode,
t.ReportingDate,
t.Value,
t.UpperBenchmark,
t.LowerBenchmark,
m.InternalId,
Frequency = m.Frequency
});
// left outer join
var failureTransactionDetail = (from p in td
join q in rd on new { ReportingDate = (DateTime)p.ReportingDate, ID = p.Frequency } equals new { q.ReportingDate, q.ID }
into LJ
//select new { p.Id, p.BatchId, p.ReportingDate, RD = q.ReportingDate, q.ID, p.Frequency });
from value in LJ.DefaultIfEmpty()
//where p.BatchId == batchId
select new {p.BatchId, p.Id, Match = 1, p.ReportingDate } into DJ
// LEFT OUTER JOIN
where DJ.BatchId == batchId
////&& DJ.Match == 0
&& !(TransactionDetailFailureReasons.Any(m => m.TransactionDetailId == DJ.Id && m.TransactionFailureReasonId == "NRD"))
&& !(TransactionDetailFailureReasons.Any(m => m.TransactionDetailId == DJ.Id && m.TransactionFailureReasonId == "RDP"))
&& !(TransactionDetailFailureReasons.Any(m => m.TransactionDetailId == DJ.Id && m.TransactionFailureReasonId == "RDF"))
select new { DJ.Id, DJ.ReportingDate, DJ.BatchId } );
My question being how i can achieve similar result as 1 AS Match does in SQL in Linq.
Could someone please guide me? Currently the SQL query returns 2 results based on Match value as null, but the LInq returns 8 results since it is not filtering the Match on Left join.
Any help is much appreciated.
Thanks in advance.
I'm just taking a stab at helping you out here because the question is a little unclear. But just comparing your sql statement to your linq query I can see that you may be trying to filter where: RD.Match IS NULL? If that assumption is correct then there is problem with your linq query.
Given the following objects:
public class TransactionDetail
{
public TransactionDetail(int id,
int batchId,
int metricCode,
DateTime reportingDate)
{
Id = id;
BatchId = batchId;
MetricCode = metricCode;
ReportingDate = reportingDate;
}
public int Id { get; }
public int BatchId { get; }
public int MetricCode { get; }
public DateTime ReportingDate { get; }
}
public class Measure
{
public Measure(int internalId,
int frequency)
{
InternalId = internalId;
Frequency = frequency;
}
public int InternalId { get; }
public int Frequency { get; }
}
public class Tmp
{
public Tmp(int id,
DateTime reportingDate)
{
Id = id;
ReportingDate = reportingDate;
}
public int Id { get; }
public DateTime ReportingDate { get; }
}
Sample Code:
static void Main(string[] args)
{
var transactionDetails = new List<TransactionDetail>
{
new TransactionDetail(id: 1, batchId: 1, metricCode: 1, reportingDate: new DateTime(2019, 1, 1)),
new TransactionDetail(id: 2, batchId: 1, metricCode: 2, reportingDate: new DateTime(2019, 1, 1))
};
var matches = new List<Measure>
{
new Measure(internalId: 1, frequency: 1),
new Measure(internalId: 2, frequency: 3)
};
var temporaryList = new List<Tmp>
{
new Tmp(1, new DateTime(2019, 1, 1)),
};
var transDetails = transactionDetails.Join(
matches,
t => t.MetricCode,
m => m.InternalId,
(t, m) => new
{
t.Id,
t.BatchId,
t.MetricCode,
t.ReportingDate,
m.InternalId,
m.Frequency
})
.ToList();
var failureTransactionDetail = transDetails
.GroupJoin(temporaryList,
trandetail => new { trandetail.ReportingDate, Id = trandetail.Frequency },
tmp => new { tmp.ReportingDate, tmp.Id },
(trandetail, tmp) => new { trandetail, tmp })
.SelectMany(t => t.tmp.DefaultIfEmpty(), (t, value) => new { t, value, Matches = 1 })
.Where(arg => !arg.t.tmp.Any());
Console.WriteLine(JsonConvert.SerializeObject(failureTransactionDetail, Formatting.Indented));
Console.ReadLine();
}
Examine the output and you'll see that you don't need Match = 1. .Where(arg => !arg.t.tmp.Any()) would be the equivalent to RD.Match IS NULL in your sql query.
Hope that puts you in the right direction.
I am having fun with LINQ query and kind of stuck at figuring out the correct method to get Count of associated entries.
I have below LINQ query and
var result = (from OR in orders
join OE in order_entries on OR.id equals OE.order_id into temp
from LOE in temp.DefaultIfEmpty()
group LOE by new {OR.user_id, OR.site } into g
select new {
col1 = g.Key.user_id,
col2 = g.Key.site,
count = g.Count() ,
cost = g.Sum( oe => oe.cost)
}
);
this turns to
SELECT
1 AS [C1],
[GroupBy1].[K1] AS [user_id],
[GroupBy1].[K2] AS [site],
[GroupBy1].[A1] AS [C2],
[GroupBy1].[A2] AS [C3]
FROM ( SELECT
[Extent1].[user_id] AS [K1],
[Extent1].[site] AS [K2],
COUNT(1) AS [A1],
SUM([Extent2].[cost]) AS [A2]
FROM [dbo].[orders] AS [Extent1]
LEFT OUTER JOIN [dbo].[order_entries] AS [Extent2] ON [Extent1].[id] = [Extent2].[order_id]
GROUP BY [Extent1].[user_id], [Extent1].[site]
) AS [GroupBy1]
What I am trying to acheive here is replace Count(1) with Count([Extent2].[id]) so in case when there is no entries associated with the order I want to show 0 instead of 1.
Can someone please help me with updating the LINQ query to achieve this ?
UPDATE :
replace with below will return the outcome for what I wanted but this also turns my sql query to perform slower..
g.Where(i => i.orders != null).Count(),
The simplest way is to use subqueries:
var qry = from o in orders
select new {
oid = o.ID,
uid = o.UserId,
site = o.Site,
count = order_entries.Where(oe=>oe.OrderId == o.ID).Count(),
cost = order_entries.Where(oe=>oe.OrderId == o.ID).Sum(oe=>oe.Cost)
};
But if you would like to join two data sets, use this:
var qry = (from o in orders join oe in order_entries on o.ID equals oe.OrderId into grp
from g in grp.DefaultIfEmpty()
select new{
oid = o.ID,
uid = o.UserId,
site = o.Site,
count = grp.Count(),
cost = grp.Sum(e=>e.Cost)
}).Distinct();
I strongly believe in it that second query could be writen in simplest way by using group statement.
Here is a complete LinqPad sample:
void Main()
{
List<TOrder> orders = new List<TOrder>{
new TOrder(1, 1, "Site1"),
new TOrder(2, 1, "Site1"),
new TOrder(3, 2, "Site2"),
new TOrder(4, 2, "Site2"),
new TOrder(5, 3, "Site3")
};
List<TOrderEntry> order_entries = new List<TOrderEntry>{
new TOrderEntry(1, 1, 5.5),
new TOrderEntry(2, 1, 6.2),
new TOrderEntry(3, 1, 4.9),
new TOrderEntry(4, 1, 55.15),
new TOrderEntry(5, 1, 0.97),
new TOrderEntry(6, 2, 2.23),
new TOrderEntry(7, 2, 95.44),
new TOrderEntry(8, 2, 3.88),
new TOrderEntry(9, 2, 7.77),
new TOrderEntry(10, 3, 25.23),
new TOrderEntry(11, 3, 31.13),
new TOrderEntry(12, 4, 41.14)
};
// var qry = from o in orders
// select new {
// oid = o.ID,
// uid = o.UserId,
// site = o.Site,
// count = order_entries.Where(oe=>oe.OrderId == o.ID).Count(),
// cost = order_entries.Where(oe=>oe.OrderId == o.ID).Sum(oe=>oe.Cost)
// };
// qry.Dump();
var qry = (from o in orders join oe in order_entries on o.ID equals oe.OrderId into grp
from g in grp.DefaultIfEmpty()
//group g by g into ggg
select new{
oid = o.ID,
uid = o.UserId,
site = o.Site,
count = grp.Count(),
cost = grp.Sum(e=>e.Cost)
}).Distinct();
qry.Dump();
}
// Define other methods and classes here
class TOrder
{
private int iid =0;
private int uid =0;
private string ssite=string.Empty;
public TOrder(int _id, int _uid, string _site)
{
iid = _id;
uid = _uid;
ssite = _site;
}
public int ID
{
get{return iid;}
set{iid = value;}
}
public int UserId
{
get{return uid;}
set{uid = value;}
}
public string Site
{
get{return ssite;}
set{ssite = value;}
}
}
class TOrderEntry
{
private int iid = 0;
private int oid = 0;
private double dcost = .0;
public TOrderEntry(int _iid, int _oid, double _cost)
{
iid = _iid;
oid = _oid;
dcost = _cost;
}
public int EntryId
{
get{return iid;}
set{iid = value;}
}
public int OrderId
{
get{return oid;}
set{oid = value;}
}
public double Cost
{
get{return dcost;}
set{dcost = value;}
}
}
I'm attempting to translate the following SQL statement to Linq and am having trouble with the multiple joins- I seem to be missing something.
SELECT DISTINCT
Test1 = Table1.Column1,
Test2 = 1,
Test3 = Table1.Column2,
Test4 = Table1.Column5,
Test5 = Table1.Column6
FROM Table1
LEFT JOIN Table2 ON Table1.Column1 = Table2.Column1
INNER JOIN Table3 ON Table1.Column3 = Table3.Column3
WHERE Table3.Column4 IN (1,2,6)
Here's the Linq so far:
var TestQuery = Table1_Collection.Select(x => new
{
Test1 = Table1.Column1,
Test2 = 1,
Test3 = Table1.Column2,
Test4 = Table1.Column5,
Test5 = Table1.Column6
})
[joins go here]
.Where("where stuff goes here");
Any ideas? I'm not so much seeking assistance with the .Where as I am the joins. I'm not sure about the formatting with the method syntax.
Here you go:
var results = Table3_Collection
.Where(i => column4s.Contains(i.Column4))
.Join(Table1_Collection, i => i.Column3, i => i.Column3, (i, j) => j)
.Join(Table2_Collection, i => i.Column1, i => i.Column1, (i, j) => i)
.Distinct(comparer);
In your original SQL query you weren't using selecting any columns from Table2, so you could omit that join. I included it above, but please feel free to remove it.
Also, your C# example didn't have Distinct, but I included it for you as it was in your original SQL query, and is most likely your intent. And, please, don't forget to implement your own IEqualityComparer. Here is an example of one:
class Table1Comparer : IEqualityComparer<Table1>
{
public bool Equals(Table1 x, Table1 y)
{
return x.Column1 == y.Column1
&& x.Column2 == y.Column2
&& x.Column3 == y.Column3
&& x.Column4 == y.Column4
&& x.Column5 == y.Column5
&& x.Column6 == y.Column6;
}
public int GetHashCode(Table1 obj)
{
return obj.GetHashCode();
}
}
try this example, I hope you help
class Table1
{
public int Id1 { get; set; }
public string Column1 { get; set; }
}
class Table2
{
public int Id2 { get; set; }
public string Column2 { get; set; }
}
class Table3
{
public int Id3 { get; set; }
public string Column3 { get; set; }
}
static void Main(string[] args)
{
var table1 = new List<Table1>();
var table2 = new List<Table2>();
var table3 = new List<Table3>();
for (int i = 0; i < 10; i++)
{
table1.Add(new Table1 { Id1 = i, Column1 = "column1_table1_" + i });
table2.Add(new Table2 { Id2 = i, Column2 = "column2_table2_" + i });
table3.Add(new Table3 { Id3 = i, Column3 = "column3_table3_" + i });
}
var table1JoinTable2 = table1.Join(table2, t1 => t1.Id1, t2 => t2.Id2, (t1, t2) => new { Id = t1.Id1, Column1 = t1.Column1, Column2 = t2.Column2 } );
var table1JoinTable2JoinTable3 = table1JoinTable2.Join(table3, t12 => t12.Id, t3 => t3.Id3, (t12, t3) => new { Id = t12.Id, Column1 = t12.Column1, Column2 = t12.Column2, Column3 = t3.Column3 });
var result1 = table1JoinTable2JoinTable3.Single(t123 => t123.Id == 1);
Console.WriteLine("Id={0} C1={1} C2={2} C3={3}", result1.Id, result1.Column1, result1.Column2, result1.Column3);
// prints "Id=1 C1=column_table1_1 C2=column_table2_1 C3=column_table3_1"
}
Here is an example of your original SQL statement in LINQ query syntax.
List<int> vals = new List<int> {1,2,6};
var qry = from rec1 in Table1
join rec2 in Table2 on rec1.Column1 equals rec2.Column2 into ljT2
from rec2 in ljT2.DefaultIfEmpty() //Handle left join
join rec3 in Table3 on rec1.Column1 equals rec3.Column3
where vals.Contains(rec3.Column4)
select new {
Test1 = rec1.Column1,
Test2 = 1,
Test3 = rec2 == null?null:rec2.Column2, //Must allow for rec2 to be null
Test4 = rec3.Column5,
Test5 = rec3.Column6
}
qry = qry.Distinct();
I have searched through many topics, find some relevant answers too, but I am still not able to reach to a solution, hence I am posting this question.
Problem Description
EmployeeResponse1 = [{"Ques":"1","Rating":"7"},{"Ques":"2","Rating":"1"},{"Ques":"3","Rating":"6"},{"Ques":"4","Rating":"1"},{"Ques":"5","Rating":"1"},{"Ques":"6","Rating":"1"},{"Ques":"7","Rating":"7"},{"Ques":"8","Rating":"1"},{"Ques":"9","Rating":"1"},{"Ques":"10","Rating":"1"},{"Ques":"11","Rating":"1"},{"Ques":"12","Rating":"1"},{"Ques":"13","Rating":"1"},{"Ques":"14","Rating":"1"},{"Ques":"15","Rating":"1"},{"Ques":"16","Rating":"10"}]
EmployeeResponse2 = [{"Ques":"1","Rating":"5"},{"Ques":"2","Rating":"4"},{"Ques":"3","Rating":"7"},{"Ques":"4","Rating":"8"},{"Ques":"5","Rating":"5"},{"Ques":"6","Rating":"9"},{"Ques":"7","Rating":"10"},{"Ques":"8","Rating":"4"},{"Ques":"9","Rating":"9"},{"Ques":"10","Rating":"6"},{"Ques":"11","Rating":"6"},{"Ques":"12","Rating":"6"},{"Ques":"13","Rating":"7"},{"Ques":"14","Rating":"7"},{"Ques":"15","Rating":"9"},{"Ques":"16","Rating":"8"}]
I have these two JSON strings in c# (there can be more).. Now I want to make a final JSON string which will be like:
EmployeeResponseAvg = [{"Ques":"1","Rating":"6"},{"Ques":"2","Rating":"2.5"},{"Ques":"3","Rating":"6.5"},{"Ques":"4","Rating":"4.5"},{"Ques":"5","Rating":"3"},{"Ques":"6","Rating":"5"},{"Ques":"7","Rating":"8.5"},{"Ques":"8","Rating":"2.5"},....,{"Ques":"16", "Rating": "9"}]
like I want rating of Ques = 1 should be the average of Rating (Ques = 1 of string 1) and Rating (Ques = 1 of string 2)... similarly for other questions
i.e like FINAL =[{ QUES = 1, RATING = (Emp1(Rating.WHERE(QUES = 1), Emp2(Rating.WHERE(QUES = 1),).AVERAGE),....................}]
Work So Far
MODEL -> SurveyResponse.cs
public class SurveyResponse
{
public string Ques { get; set; }
public string Rating { get; set; }
}
public class ResponseDataCalls
{
public static SurveyResponse PutData(string t, string v)
{
SurveyResponse s = new SurveyResponse();
s.Ques = t;
s.Rating = v;
return s;
}
}
WebAPI RevGroupChartController.cs
public class RevGroupChartController : ApiController
{
private hr_toolEntities _db = new hr_toolEntities();
public object Get(int cid, int gid)
{
spiderChart obj = new spiderChart();
var group_employees = (from ge in _db.hrt_group_employee
where ge.fk_group_id == gid
select ge.fk_employee_id).ToList();
List<string> EMP = new List<string>();
List<string> SUP = new List<string>();
List<SurveyResponse> EmpResponse = new List<SurveyResponse>();
List<SurveyResponse> SupResponse = new List<SurveyResponse>();
List<List<SurveyResponse>> tmpEMP = new List<List<SurveyResponse>>();
List<List<SurveyResponse>> tmpSUP = new List<List<SurveyResponse>>();
foreach(var emp in group_employees)
{
int eid = Convert.ToInt32(emp);
var Data = (from d in _db.hrt_cycle_response
join g in _db.hrt_cycle_groups on d.hrt_cycle.pk_cycle_id equals g.fk_cycle_id
where d.fk_cycle_id == cid && g.fk_group_id == gid && d.fk_employee_id == eid
select new
{
d.response_employee_answers,
d.response_supervisor_answers
}).First();
EMP.Add(Data.response_employee_answers);
SUP.Add(Data.response_supervisor_answers);
}
foreach(var e in EMP)
{
//tmpEMP = new JavaScriptSerializer().Deserialize<TEMP>(e);
var s = new JavaScriptSerializer();
List<SurveyResponse> em = s.Deserialize<List<SurveyResponse>>(e);
tmpEMP.Add(em);
}
foreach (var s in SUP)
{
//tmpSUP = new JavaScriptSerializer().Deserialize<TEMP>(s);
var e = new JavaScriptSerializer();
List<SurveyResponse> sp = e.Deserialize<List<SurveyResponse>>(s);
tmpSUP.Add(sp);
}
var empl = _db.hrt_questions.Select(x => new { x.question_name }).ToList();
List<int[]> Emprating = new List<int[]>();
//int avgRating;
int cnt = 0;
foreach(var item in tmpSUP)
{
int noofQ = item.Count;
int[] i = new int[noofQ];
for (int y = 0; y > tmpSUP.Count; y++)
{
i[y] = Convert.ToInt32(item[cnt].Rating);
}
Emprating.Add(i);
cnt++;
}
//obj.Employee = Data.response_employee_answers;
//obj.Supervisor = Data.response_supervisor_answers;
obj.ques = new List<object>();
for (int i = 0; i < empl.Count; i++)
{
obj.ques.Add(empl[i].question_name);
}
return obj;
}
public class TEMP
{
public List<SurveyResponse> data { get; set; }
}
}
Explanation of Code
I pass a cycle ID and a group ID...
Each group has more than 1 employee and each employee has a supervisor
so if say group ID 1023 has 2 employees.
Now we have 2 employees and 2 supervisors
we have a json record for each of them
LIKE DB TABLE RESPONSE {fk_emp_id, fk_sup_id, cycle_id, emp_reponse(json), supervisor_response(json)}
so I need to make ONE JSON string for employees (which contains the average of all ratings)
and ONE JSON string for SUPERVISOR (again, average of both the JSONs)
there could be any number of employees, depending on the group size
and each employee will always have a supervisor
In short I want a a string like:
FinalEmployeeResponse = [{'Ques': '1', 'Rating': 'R1'}, {'Ques': '2', 'Rating': 'R2'}, {'Ques': '3', 'Rating': 'R3'}, {'Ques': '4', 'Rating': 'R4'}, ........, {'Ques': '16', 'Rating': 'R16'}]
Here, R1 = AVERAGE(Emp1json.Rating.WHERE('Ques' = 1), Emp2json.Rating.WHERE('Ques' = 1), .....)
and
R2 = AVERAGE(Emp1json.Rating.WHERE('Ques' = 2), Emp2json.Rating.WHERE('Ques' = 2), .....)
... and so on....
Looking forward to your responses.
I am new on stack overflow, please ask for more details if I have missed something.
The correct way to do this is to parse this as JSON. The quick and dirty way is:
static void Main(string[] args)
{
string json1 = #"[{""Ques"":""1"",""Rating"":""7""},{""Ques"":""2"",""Rating"":""1""},{""Ques"":""3"",""Rating"":""6""},{""Ques"":""4"",""Rating"":""1""},{""Ques"":""5"",""Rating"":""1""},{""Ques"":""6"",""Rating"":""1""},{""Ques"":""7"",""Rating"":""7""},{""Ques"":""8"",""Rating"":""1""},{""Ques"":""9"",""Rating"":""1""},{""Ques"":""10"",""Rating"":""1""},{""Ques"":""11"",""Rating"":""1""},{""Ques"":""12"",""Rating"":""1""},{""Ques"":""13"",""Rating"":""1""},{""Ques"":""14"",""Rating"":""1""},{""Ques"":""15"",""Rating"":""1""},{""Ques"":""16"",""Rating"":""10""}]";
string json2 = #"[{""Ques"":""1"",""Rating"":""5""},{""Ques"":""2"",""Rating"":""4""},{""Ques"":""3"",""Rating"":""7""},{""Ques"":""4"",""Rating"":""8""},{""Ques"":""5"",""Rating"":""5""},{""Ques"":""6"",""Rating"":""9""},{""Ques"":""7"",""Rating"":""10""},{""Ques"":""8"",""Rating"":""4""},{""Ques"":""9"",""Rating"":""9""},{""Ques"":""10"",""Rating"":""6""},{""Ques"":""11"",""Rating"":""6""},{""Ques"":""12"",""Rating"":""6""},{""Ques"":""13"",""Rating"":""7""},{""Ques"":""14"",""Rating"":""7""},{""Ques"":""15"",""Rating"":""9""},{""Ques"":""16"",""Rating"":""8""}]";
string averages = AverageNodes(json1, json2);
Console.WriteLine(averages);
Console.ReadKey();
}
private static string AverageNodes(params string[] json)
{
var regex = new Regex(#"(""Ques"":""(?<question>\d+)"",""Rating"":""(?<rating>\d+)"")", RegexOptions.ExplicitCapture | RegexOptions.IgnoreCase);
var ANUs = regex.Matches(string.Join("", json))
.Cast<Match>()
.Select(m => new { Question = m.Groups["question"].Value, Rating = int.Parse(m.Groups["rating"].Value) })
.GroupBy(a => a.Question, a => a.Rating)
.Select(a => string.Format("{{\"Ques\":\"{0}\",\"Rating\":\"{1}\"}}", a.Key, a.Average()));
return "[" + string.Join(",", ANUs) + "]";
}
I found a 1 line answer to this using LINQ.
double _avg1 = tmpEMP.Select(x => Convert.ToInt32(x.ElementAt(i).Rating)).Average();