这是我在ASP.NET Core中使用Entity Framework Core的第一个C#应用程序,我尝试在Linq中编写以下MySQL查询:
SELECT districtid, COUNT(id), MONTH(registerDate)
FROM StreetCrimes
GROUP BY districtid, MONTH(registerDate)
我面临的问题是编写linq代码。我已尝试以下方法来接近答案:
var StreetCrimes = this.database.StreetCrimes
.Select(s => s.district)
.Count(s => s.id > 0)
.GroupBy(s => s.district)
.ThenBy(s => s.registerDate);
任何帮助实现这一目标的人都将不胜感激。
我的StreetCrime
课程:
public class StreetCrime
{
public int id { get; set; }
public string caseNumber { get; set; }
public District district { get; set; }
public DateTime registerDate { get; set; }
public TimeSpan crimeTime { get; set; }
}
答案 0 :(得分:2)
District需要继承IEqualityComparer才能使GroupBy正常工作。 linq应该是这样的:
var StreetCrimes = this.database.StreetCrimes
.Where(x => x.id > 0)
.GroupBy(s => new {district = s.district, date = s.registerDate})
.Select(x => new { count = x.Count(), district = x.Key.district, date = x.Key.date})
.ToList();
答案 1 :(得分:0)
你可以使用LINQ
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
mspin=(Spinner) findViewById(R.id.spinner1);
submit = (Button) findViewById(R.id.btn1);
layout = (LinearLayout) findViewById(R.id.linear);
lay = (LinearLayout) findViewById(R.id.li);
edt=(EditText) findViewById(R.id.ed2);
sc = (ScrollView) findViewById(R.id.sc1);
int no = 20;
allEds = new ArrayList<EditText>();
for (int i=1;i<=no;i++){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
edtAdd = new EditText(SelectActivity.this);
layout.setLayoutParams(params);
allEds.add(edtAdd);
edtAdd.setHint("Enter Name" + i);
edtAdd.setId(i);
layout.addView(edtAdd);
}
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (allEds.size()<=9) {
Intent data = new Intent(SelectActivity.this, HalfPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str); //you don't need to keep this in loop as its same.
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);
}
startActivity(data);
}
else {
Intent data = new Intent(SelectActivity.this, FullPieChartActivity.class);
String[] items = new String[allEds.size()];
String str = String.valueOf(allEds.size());
data.putExtra("size", str);// this is the line where I sent that count
data.putExtra("edt", edt.getText().toString());
for (int j = 0; j < allEds.size(); j++) {
items[j] = allEds.get(j).getText().toString();
data.putExtra("edData" + j, items[j]);//here is filled data send line
}
startActivity(data);
}
}
});
}
}
但您也可以使用 var StreetCrimes = this.database.StreetCrimes
.Where(x => x.id > 0)
.GroupBy(s => new {district = s.district, date = s.registerDate})
.Select(x => new { count = x.Count(), district = x.Key.district, date = x.Key.date})
.ToList();
。
DataTable
或者可能有两行:
SqlCommand cmd = new SqlCommand("SELECT districtid,COUNT(id),MONTH(registerDate) FROM StreetCrimes GROUP BY districtid,MONTH(registerDate)",connectionString)
DataTable dt = new DataTable();
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt)
int crimeCount = dt.Rows.Count();
答案 2 :(得分:0)
感谢@jdweng,我更接近我的回答。但是我需要每个月的总金额。
var StreetCrimes = this.database.StreetCrimes
.Where(x => x.id > 0)
.GroupBy(s => new {district = s.district, date = s.registerDate.Month})
.Select(x => new { count = x.Count(), district = x.Key.district, date = x.Key.date})
.ToList();
答案 3 :(得分:0)
这应该有效:
var StreetCrimes = this.database.StreetCrimes
.Where(x => x.id > 0)
.GroupBy(s => new {district = s.district, month = s.registerDate.Month})
.Select(x => new { count = x.Count(), district = x.Key.district, month = x.Key.month})
.ToList();