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

本文关键字:字符串 查询 查找 索引 Linq | 更新日期: 2023-09-27 18:03:06

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

匹配
_objProductdiscount.productid = 1

在第2位

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();
    }
}

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

font - family:宋体

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] ...