我正在尝试根据以下sql创建linq查询。
找不到一个包含多个表的联接以及一个分组来自不同表的字段的示例。
select t2.field1, t1.field1, count(t2.field1) from Table1 t1
join Table2 t2 on t1.pkfield = t2.pkfield
group by t2.field1, t1.field1
having count(*) > 1
order by t2.pkfield1
尝试了以下内容:
var test =
from t1 in db.Table1
join t2 in db.Table2 on t1.pkfield equals t2.pkfield
group x by (t1.field1, t2.field1)
select t1.field1, t2.field2
答案 0 :(得分:1)
您没有发布实体类和输入示例,因此我无法验证我的代码。但我希望这个概念清楚
>Rosalind_4451
TGTGCTGAGAATTTGCACCACTCCCGTGTCGATGGTATGAGGGAACTGGTTGTAGAACTG
CGACATTACTTTCTGAGACGCAAGGCGTACAGCCAAATGTGAAGTACAAACCAAGAAGTT
CGGGCCGGTCGTTAAGTATCCCGCATACCACACCTTGAATGGAACCCGTCGGGCCCGGGT
TTTTTAAGGGGTTTAACACACCAGACTAAGGCGTTTTCTGACTTGGAGTCGAGTCGATGG
GATGTACCTATCGAAAGAGGTACTCTATATTTGTACCGTGCAG
>Rosalind_6341
CGACAGGCTCTTTAAATTGCGAAGTGTGCCATCCTACCGCATATGCCTAGCCTAATCTTG
AGCTCGAAACCGGAAATGGAAGCACACCAATGTCTCTCCGAGCTACTGTCCATCTACGTC
ATTGCTGACGTCATATGGGCATTAGTTCCTAATCGTGATAGCGAAGGCGGTTGCGTATAC
GGCTGGTGTGTTCGGGCCAATAACGTAGAAGTATCAACATGACAGCCGTCGTGGGGAACC
在txt = open (input ())
lines = txt.readlines ()
for lines in txt:
if lines == ">Rosalind_9690":
#fin = txt.seek(">Rosalind_9690")
#data1= fin.readlines("Rosalind_4299" to "Rosalind_9690")
a = lines.count("A")
g = lines.count("G")
c = lines.count("C")
t = lines.count("T")
float (a)
float (g)
float (c)
float (t)
CG_con = (g+c)/(a+g+c+t)
print (CG_con)
上方的代码中,您的自定义db.Table1.SelectMany(t1 => db.Table2.Select(t2 => new { t2.field1, t1.field1, t2.pkfield1 }))
.GroupBy(x => new { t1Field1 = t2.field1, t2Field1 = t1.field1 })
.Where(g => g.Count() > 1)
.OrderBy(g => g.Min(x => x.pkfield1))
.Select(g => new { g.Key.t1Field1, g.Key.t2Field1, g.Count() });
和db
将被翻译为DbContext
答案 1 :(得分:0)
我不知道如何在单个查询中执行此操作,但是我已经完成了类似的操作,如下所示。希望它能帮助您或至少指向正确的方向。
var result = from p in person
join o in orders on p.OrderId equals o.OrderId
// build your custom object with columns from multiple tables.
select new { o.OrderId, p.OrderId};
// now group the result
var grouped = result.GroupBy(x => x.OrderId)
.Where(y=> y.Count() > 1);