从数据库中选择带有linq查询的名字的第一个字母

时间:2015-07-13 15:28:25

标签: c# asp.net linq

我想选择名字的第一个字母并更改字体权重,如下面的代码;

  LinearLayout layout = new LinearLayout(getActivity());
    TextView textView = new TextView(getActivity());
    textView.setVisibility(View.VISIBLE);
    textView.setText("صيققق٣يفاوووووومالؤؤتمكا");
    layout.addView(textView);
    layout.setBackgroundColor(getActivity().getResources().getColor(R.color.transparent));
    textView.setTextColor(getResources().getColor(R.color.white));
    layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
    Bitmap bitmap = Bitmap.createBitmap(layout.getMeasuredWidth(), layout.getMeasuredHeight(), Bitmap.Config.RGB_565);
    //layout.draw(canvas);
    Canvas canvas = new Canvas(bitmap);
    layout.layout(0,0, canvas.getWidth(), canvas.getHeight());
    layout.draw(canvas);

2 个答案:

答案 0 :(得分:1)

解决方案

     List<string> Capitals = (from u in db.Users
                                 select u.Name[0].ToString().ToUpper()).Distinct().ToList();


        telList.Items.Clear();

        for (int i = 0; i < alpha.Length; i++)
        {




            ListItem listItem = new ListItem(Convert.ToString(alpha[i]));
            listItem.Attributes.Add("value", Convert.ToString(i));

            for (int j = 0; j < Capitals.Count; j++)
            {
                if (Convert.ToString(alpha[i]) == Capitals[j])
                {
                    listItem.Attributes.Add("class", "class1");
                    break;
                }
                else
                {
                    listItem.Attributes.Add("class", "class2");
                }
            }



            telList.Items.Add(listItem);
        }

答案 1 :(得分:0)

var s = db.Users.Select(x =>  x.Name.Substring(0,1)).FirstOrDefault();

telList.Items.Clear();

for (int i = 0; i < alpha.Length; i++) {
    var listItem = new ListItem(Convert.ToString(alpha[i]));
    listItem.Attributes.Add("value", Convert.ToString(i));

    if (Convert.ToString(i) == s) {
        listItem.Attributes.Add("class", "class2");
    } else {
        listItem.Attributes.Add("class", "class1");
    }
    telList.Items.Add(listItem);
}

可替换地:

var s = db.Users.Select(x =>  x.Name.Substring(0,1)).FirstOrDefault();
telList.Items.Clear();
telList.Attributes.Add("data-firstletter",s);
telList.Items.AddRange(alpha.Select((letter,index)=>new ListItem(letter,index.ToString()));

然后使用css设置你的listitem样式(假设你的select具有id telList):

#telList > li { font-weight: normal; }
#telList[data-firstletter=A] > li[value=0],
#telList[data-firstletter=B] > li[value=1],
#telList[data-firstletter=C] > li[value=2],
#telList[data-firstletter=D] > li[value=3],
#telList[data-firstletter=E] > li[value=4],
#telList[data-firstletter=F] > li[value=5],
...
#telList[data-firstletter=Z] > li[value=25] {
  font-weight: bold;
}