我正在编写linq查询以从表中获取一些细节。对于每个"notVerified"
,我希望clientID
计算ID clientId clientName empID docStatus
1 IN1001 Infy 100 Verified
2 IN1001 Infy 101 notVerified
3 IN1001 Infy 102 notVerified
。
我的表格结构如下
clientID clientName Count
IN1001 Infy 2
查询后我想要下面表格中的数据
var noofRecords = (from c in db.ts_upld_doc
group c by c.upld_docid into grouping
select new
{
key = grouping.Key,
Count = grouping.Count()
});
return noofRecords.Count();
这是我在下面尝试的。我能够得到数,但我应该在下面的查询中放置条件。
package com.example.dekrinssoft.contactsbackup;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Button backup, restore;
String vfile;
FileOutputStream mFileOutputStream = null;
Cursor cursor;
ArrayList<String> vCard;
File f;
String storage_path;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backup = (Button) findViewById(R.id.btnbkp);
restore = (Button) findViewById(R.id.btnres);
vfile = "contacts.vcf";
storage_path = Environment.getExternalStorageDirectory().toString() + "/" + vfile;
f = new File(storage_path);
backup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new LongOperation().execute("");
}
});
restore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent intent = new Intent();
final MimeTypeMap mime = MimeTypeMap.getSingleton();
String tmptype = mime.getMimeTypeFromExtension("vcf");
final File file = new File(Environment.getExternalStorageDirectory().toString() + "/contacts.vcf");
intent.setDataAndType(Uri.fromFile(file), tmptype);
startActivity(intent);
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
ProgressDialog progress;
@Override
protected void onPreExecute() {
progress = new ProgressDialog(MainActivity.this);
progress.setMessage("Loading.... ");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.setProgress(0);
progress.show();
}
@Override
protected String doInBackground(String... params) {
try {
if (!f.exists())
f.createNewFile();
mFileOutputStream = new FileOutputStream(storage_path, false);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
getVcardString();
return "Executed";
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, " Succesfully backed up", Toast.LENGTH_LONG).show();
progress.dismiss();
}
}
private void getVcardString() {
vCard = new ArrayList<String>();
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null && cursor.getCount() > 0) {
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
get(cursor);
Log.d("TAG", "Contact " + (i + 1) + "VcF String is" + vCard.get(i));
cursor.moveToNext();
}
} else {
Log.d("TAG", "No Contacts in Your Phone");
}
try {
mFileOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void get(Cursor cursor) {
//cursor.moveToFirst();
String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd;
try {
fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
byte[] buf = new byte[(int) fd.getDeclaredLength()];
fis.read(buf);
String vcardstring = new String(buf);
vCard.add(vcardstring);
mFileOutputStream.write(vcardstring.toString().getBytes());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
提前谢谢。
答案 0 :(得分:1)
您可能会在分组之前放置where
子句的内容(为什么要对要过滤的这些项进行分组)。然后,如果您想要按照指定的方式输出,最简单的方法是按clientID
和clientName
分组。
同样在您的上述查询中,您按upld_docid
(ID
)字段进行分组 - 这是您数据中的唯一字段 - 意味着您的所有群组的大小均为1 - 就像您没有& #39; t group。
见下面的查询,应该做你想做的事:
var noofRecords = (from c in db.ts_upld_doc
where c.docStatus == "notVerified"
group c by new { c.clientID, c.clientName } into grouping
select new
{
ClientId = grouping.Key.clientID,
ClientName = grouping.Key.clientName,
Count = grouping.Count()
}).ToList();
答案 1 :(得分:0)
您可以将地点放在多个地方,一个选项:
var noofRecords = (from c in db.ts_upld_doc
where c.docStatus == "notVerified"
group c by c.upld_docid into grouping
select new
{
key = grouping.Key,
Count = grouping.Count()
});
return noofRecords.Count();
要意识到!!您返回的是记录数,而不是您设置为grouping.Count()
要获得预期结果,您应该执行以下操作:
var noofRecords = (from c in db.ts_upld_doc
where c.docStatus == "notVerified"
group c by new { c.clientId , c.clientName } into grouping
select new
{
ClientId = grouping.Key.clientId,
ClientName = grouping.Key.clientName,
Count = grouping.Count()
});
return noofRecords.ToList();