Linq查询查找匹配索引的值以及字符串值?

时间:2016-08-19 17:54:15

标签: c# linq

我想要获取' 2.2'在var对象中与

匹配
_objProductdiscount.productid = 1

位于第二位

public class ProductdiscountapplyEntity//1 10  1.2 10    2 20    1.2 20
{
    public string _productid;
    public string _discount;
    public ProductdiscountapplyEntity(string productid, string discount)
    {
        _discount = discount;
        _productid = productid;
    }
}

public class members //1 1.2    2 2.2
{
    public string _Productid;
    public string _assproduct;
    public members(string Productid, string assproduct)
    {
        _Productid = Productid;
        _assproduct = assproduct;
    }
}

public  class Productdiscount
{ 
     string _ProductidinPDtag;//1 2
     string _discPercent;//10 20

    public Productdiscount(string ProductidinPDtag,string discPercent)
    {
        _ProductidinPDtag = ProductidinPDtag;

        _discPercent = discPercent;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        List<Productdiscount> _objProductdiscount = new List<Productdiscount>();

        _objProductdiscount.Add(new Productdiscount("1","10%" ));
        _objProductdiscount.Add(new Productdiscount("1", "20%"));
        _objProductdiscount.Add(new Productdiscount("2", "20%"));

        List<members> _objmembers = new List<members>();

        _objmembers.Add(new members("1", "1.2"));
        _objmembers.Add(new members("1", "2.2"));
        _objmembers.Add(new members("2", "3.2"));

        List<ProductdiscountapplyEntity> _objProductdiscountapplyEntity = new List<ProductdiscountapplyEntity>();

        var myassprod = (_objmembers.Where(c => c._Productid == "1").Take(1).Select(c => c._assproduct)).FirstOrDefault();
    }
}

1 个答案:

答案 0 :(得分:0)

格式化的原文:

public class ProductdiscountapplyEntity //1 10 1.2 10 2 20 1.2 20 
{
  public string _productid;
  public string _discount;
  public ProductdiscountapplyEntity(string productid, string discount)
  {
    _discount = discount;
    _productid = productid;
  }
}

public class members // 1 1.2 2 2.2
{
  public string _Productid;
  public string _assproduct;
  public members(string Productid, string assproduct)
  {
    _Productid = Productid;
    _assproduct = assproduct;
  }
}

public class Productdiscount
{
  string _ProductidinPDtag; // 1 2
  string _discPercent; // 10 20

  public Productdiscount(string ProductidinPDtag, string discPercent)
  {
    _ProductidinPDtag = ProductidinPDtag;
    _discPercent = discPercent;
  }
}

static class Program
{
  static void Main(string[] args)
  {
    List<Productdiscount> _objProductdiscount = new List<Productdiscount>();

    _objProductdiscount.Add(new Productdiscount("1", "10%"));
    _objProductdiscount.Add(new Productdiscount("1", "20%"));
    _objProductdiscount.Add(new Productdiscount("2", "20%"));


    List<members> _objmembers = new List<members>();

    _objmembers.Add(new members("1", "1.2"));
    _objmembers.Add(new members("1", "2.2"));
    _objmembers.Add(new members("2", "3.2"));

    List<ProductdiscountapplyEntity> _objProductdiscountapplyEntity = new List<ProductdiscountapplyEntity>();

    var myassprod = (_objmembers.Where(c => c._Productid == "1").Take(1).Select(c => c._assproduct)).FirstOrDefault();

  }
}

我认为分组是选择“产品”的好主意

// --- examples ---

var group = _objmembers.GroupBy(c => c._Productid);
var groupWithProductId_1 = group.First(g => g.Key == "1");
var allProducts = groupWithProductId_1.Select(c => c._assproduct);
var myassprod_1_2 = allProducts.Skip(0).FirstOrDefault(); // 1.2
var myassprod_2_2 = allProducts.Skip(1).FirstOrDefault(); // 2.2
var myassprod_3_2 = allProducts.Skip(2).FirstOrDefault(); // null -> only elements from group (_Productid == "1")

var asArray = allProducts.ToArray(); // { "1.2", "2.2" } its better to get as an Array and can select the values directly asArray[0], asArray[1] ...