要在一个list<gz>中,先查询gp.hujun的值是否等于gz.quzhi,若有,则返回gz.defen的值,若没有,则查询gp.hujun小于list<gz>中gz.quzhi的最大值。示例数据:gz.quzhi=1,gz.defen=5,gz.guzhi=2,gz.defen=7,gz.quzhi=3,gz.defen=6....,若,gp.hujun=4,查询结果要求返回gz.defen=6,若gp.hunjun=0.5,要求返回gz.defen=5.[/size]
10 个解决方案
#1
你的问题是什么,循环不会写?大于小于判不来?
#2
list.Where(x.quzhi<=4).OrderByDescending(x=>x.quzhi).FirstOrDefault();
#3
用你的方法会出没有匹配的规则,程序报错!
#4
public ActionResult CountDeFen(int id)
{
var gp = db.gupiao.Find(id);
List<attribName> attList = db.attribName.Where(p => true).ToList();
List<GuiZhe> gzList = new List<GuiZhe>();
double? deFen = 0;
foreach(var att in attList)
{
switch (att.属性名称) {
case "户均持股比例":
// if()
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
//list.Where(x.quzhi <= 4).OrderByDescending(x => x.quzhi).FirstOrDefault();
deFen+=gzList.Where(x => x.规则取值 <= gp.户均持股比例).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "总市值":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.总市值).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "风险系数":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.风险系数).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
后面略........
{
var gp = db.gupiao.Find(id);
List<attribName> attList = db.attribName.Where(p => true).ToList();
List<GuiZhe> gzList = new List<GuiZhe>();
double? deFen = 0;
foreach(var att in attList)
{
switch (att.属性名称) {
case "户均持股比例":
// if()
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
//list.Where(x.quzhi <= 4).OrderByDescending(x => x.quzhi).FirstOrDefault();
deFen+=gzList.Where(x => x.规则取值 <= gp.户均持股比例).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "总市值":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.总市值).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "风险系数":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.风险系数).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
后面略........
#5
请看图片,根据户均持股比例中的值,查询得到对应的得分。表二的规则需要动态更新,随时增加删除。我能想到上面的的方法,但与最终计算结果不同。。
#6
如果表2的规律不变,应该很容易进行比对,最多循环次数多几次。
#7
规则 就是需要变~~~,有什么好的解决方案 ?
#8
你计算出来的是某一只股票的得分,对一只股票而言我没看出有什么错,有更具体的数字来说明错误吗
#9
回复不知道跑哪去了,重贴一下
结果
603268.SH,0.1405,9
603315.SH,0.0741,7
603328.SH,0.0487,7
603333.SH,0.0909,9
603399.SH,0.0351,7
难道不应该是这样么?你那个规则定义好像不太对哦
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
List<GuMin> listGuMin = new List<GuMin>();
listGuMin.Add(new GuMin("603227.SH", 0.0204));
listGuMin.Add(new GuMin("603268.SH", 0.1405));
listGuMin.Add(new GuMin("603315.SH", 0.0741));
listGuMin.Add(new GuMin("603328.SH", 0.0487));
listGuMin.Add(new GuMin("603333.SH", 0.0909));
listGuMin.Add(new GuMin("603399.SH", 0.0351));
List<GuiZe> listGuiZe = new List<GuiZe>();
listGuiZe.Add(new GuiZe(0.01,0,3));
listGuiZe.Add(new GuiZe(0.03,0.01,5));
listGuiZe.Add(new GuiZe(0.08,0.03,7));
listGuiZe.Add(new GuiZe(double.MaxValue,0.08,9));
var query = from n in listGuMin from m in listGuiZe where n.户均持股比例 <= m.最大值 && n.户均持股比例 > m.最小值 select new { n.证券代码, n.户均持股比例, m.得分 };
foreach (var n in query)
{
Console.WriteLine(n.证券代码 + "," + n.户均持股比例 + "," + n.得分);
}
}
}
class GuMin{
public string 证券代码 { get; set; }
public double 户均持股比例 { get; set; }
public GuMin(string zjdm,double cgbl)
{
this.证券代码 = zjdm;
this.户均持股比例 = cgbl;
}
}
class GuiZe
{
public double 最小值 { get; set; }
public double 最大值 { get; set; }
public int 得分 { get; set; }
public GuiZe(double max, double min, int defen)
{
this.最小值 = min;
this.最大值 = max;
this.得分 = defen;
}
}
结果
603268.SH,0.1405,9
603315.SH,0.0741,7
603328.SH,0.0487,7
603333.SH,0.0909,9
603399.SH,0.0351,7
难道不应该是这样么?你那个规则定义好像不太对哦
#10
感谢回复,如果不那样设规则,该怎么样设置?
#1
你的问题是什么,循环不会写?大于小于判不来?
#2
list.Where(x.quzhi<=4).OrderByDescending(x=>x.quzhi).FirstOrDefault();
#3
用你的方法会出没有匹配的规则,程序报错!
#4
public ActionResult CountDeFen(int id)
{
var gp = db.gupiao.Find(id);
List<attribName> attList = db.attribName.Where(p => true).ToList();
List<GuiZhe> gzList = new List<GuiZhe>();
double? deFen = 0;
foreach(var att in attList)
{
switch (att.属性名称) {
case "户均持股比例":
// if()
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
//list.Where(x.quzhi <= 4).OrderByDescending(x => x.quzhi).FirstOrDefault();
deFen+=gzList.Where(x => x.规则取值 <= gp.户均持股比例).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "总市值":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.总市值).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "风险系数":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.风险系数).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
后面略........
{
var gp = db.gupiao.Find(id);
List<attribName> attList = db.attribName.Where(p => true).ToList();
List<GuiZhe> gzList = new List<GuiZhe>();
double? deFen = 0;
foreach(var att in attList)
{
switch (att.属性名称) {
case "户均持股比例":
// if()
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
//list.Where(x.quzhi <= 4).OrderByDescending(x => x.quzhi).FirstOrDefault();
deFen+=gzList.Where(x => x.规则取值 <= gp.户均持股比例).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "总市值":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.总市值).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
case "风险系数":
gzList = db.GuiZhe.Where(p => p.评测ID == att.ID).ToList();
deFen += gzList.Where(x => x.规则取值 <= gp.风险系数).OrderByDescending(x => x.规则取值).FirstOrDefault().得分;
break;
后面略........
#5
请看图片,根据户均持股比例中的值,查询得到对应的得分。表二的规则需要动态更新,随时增加删除。我能想到上面的的方法,但与最终计算结果不同。。
#6
如果表2的规律不变,应该很容易进行比对,最多循环次数多几次。
#7
规则 就是需要变~~~,有什么好的解决方案 ?
#8
你计算出来的是某一只股票的得分,对一只股票而言我没看出有什么错,有更具体的数字来说明错误吗
#9
回复不知道跑哪去了,重贴一下
结果
603268.SH,0.1405,9
603315.SH,0.0741,7
603328.SH,0.0487,7
603333.SH,0.0909,9
603399.SH,0.0351,7
难道不应该是这样么?你那个规则定义好像不太对哦
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
List<GuMin> listGuMin = new List<GuMin>();
listGuMin.Add(new GuMin("603227.SH", 0.0204));
listGuMin.Add(new GuMin("603268.SH", 0.1405));
listGuMin.Add(new GuMin("603315.SH", 0.0741));
listGuMin.Add(new GuMin("603328.SH", 0.0487));
listGuMin.Add(new GuMin("603333.SH", 0.0909));
listGuMin.Add(new GuMin("603399.SH", 0.0351));
List<GuiZe> listGuiZe = new List<GuiZe>();
listGuiZe.Add(new GuiZe(0.01,0,3));
listGuiZe.Add(new GuiZe(0.03,0.01,5));
listGuiZe.Add(new GuiZe(0.08,0.03,7));
listGuiZe.Add(new GuiZe(double.MaxValue,0.08,9));
var query = from n in listGuMin from m in listGuiZe where n.户均持股比例 <= m.最大值 && n.户均持股比例 > m.最小值 select new { n.证券代码, n.户均持股比例, m.得分 };
foreach (var n in query)
{
Console.WriteLine(n.证券代码 + "," + n.户均持股比例 + "," + n.得分);
}
}
}
class GuMin{
public string 证券代码 { get; set; }
public double 户均持股比例 { get; set; }
public GuMin(string zjdm,double cgbl)
{
this.证券代码 = zjdm;
this.户均持股比例 = cgbl;
}
}
class GuiZe
{
public double 最小值 { get; set; }
public double 最大值 { get; set; }
public int 得分 { get; set; }
public GuiZe(double max, double min, int defen)
{
this.最小值 = min;
this.最大值 = max;
this.得分 = defen;
}
}
结果
603268.SH,0.1405,9
603315.SH,0.0741,7
603328.SH,0.0487,7
603333.SH,0.0909,9
603399.SH,0.0351,7
难道不应该是这样么?你那个规则定义好像不太对哦
#10
感谢回复,如果不那样设规则,该怎么样设置?