【问题标题】:select new in linq在 linq 中选择新的
【发布时间】:2011-07-19 06:56:11
【问题描述】:
var CountryCompanyDB = from b in dc.PropertyCompanies where (b.Country.Contains(txtSearch)) select;

Session["CountryCompany"] = CountryCompanyDB.ToList();

if(test==1)
{
    var result = (List<PropertyCompany >)Session["CountryCompany"];
}

效果很好

但我想要

var CountryCompanyDB = from b in dc.PropertyCompanies where (b.Country.Contains(txtSearch)) select new {b.id , b.name};

Session["CountryCompany"] = CountryCompanyDB.ToList();

if(test==1)
{
    var result = (List<PropertyCompany new {b.id , b.name}>)Session["CountryCompany"];//does not can this work
}

我想选择新的 Session["CountryCompany"] 如何执行这项工作。

编辑

class   kbc {
    public Int64 id { get; set; }
    public string  name { get; set; }
}


  var CountryCompanyDB = from b in dc.PropertyCompanies where (b.Country.Contains(txtSearch)) select new { id=b.IdCompany ,name=b.NameCompany} ;

 if(test==1)
{
    var result = (List<kbc>)Session["CountryCompany"];
}

说错误:
无法将“System.Collections.Generic.List1[&lt;&gt;f__AnonymousType02[System.Int64,System.String]]”类型的对象转换为“System.Collections.Generic.List`1[FullSearch+kbc]

【问题讨论】:

  • 没有任何评论的投票是不公平的

标签: c# asp.net linq linq-to-sql generics


【解决方案1】:

在您的 LINQ 语句中定义您的 PropertyCompany

var CountryCompanyDB = from b in dc.PropertyCompanies
                       where b.Country.Contains(txtSearch)
                       select new PropertyCompany()
                       { 
                          ID = b.id,
                          Name = b.name,
                       };

其中IDNamePropertyCompany 类的可能属性名称。

【讨论】:

  • @testcomeria:您应该基本上避免在会话中使用匿名类型。相反,请使用您尝试从中检索的相同类型,在本例中为 PropertyCompany 类。请注意,我是如何从您的原始代码中更改 select 子句的。
【解决方案2】:

只要您在本地范围内,匿名对象就可以工作。现在,一旦您将它存储在会话中并检索它,编译器就需要知道检索对象的类型。所以你可以创建一些 DTO 类型的东西来完成这个任务,比如

public class DTOCompany
{
  public int id{get;set;}
  public string name{get;set;}
}

你可以在你的 linq 查询中使用这个对象

var CountryCompanyDB = from b in dc.PropertyCompanies where (b.Country.Contains(txtSearch)) select new DTOCompany{id=b.id ,name = b.name};

当从会话中检索它时,您可以将其转换为 DTOCompany 列表,例如

var result = (List<DTOCompany>)Session["CountryCompany"];

【讨论】:

  • 无法将“System.Collections.Generic.List1[&lt;&gt;f__AnonymousType02[System.Int64,System.String]]”类型的对象转换为“System.Collections.Generic.List`1[全搜索+DTOCompany ]
  • 你可能错过了 Linq 查询中的select new DTOCompany{id=b.id ,name = b.name} 部分
【解决方案3】:

匿名类型只能用作更复杂算法的中间体。如果要将结果保存到变量中或将其传递给方法,则应创建特定的类。

【讨论】:

    【解决方案4】:

    new {b.id , b.name}是匿名类型,不能方便引用。您应该使用代码中定义的 regular 类,或者可能是 Tuple&lt;int,string&gt;(即select Tuple.Create(b.id, b.name)) - 然后:

    var result = (List<Tuple<int,string>>)Session["CountryCompany"];
    

    【讨论】:

    • @testcomeria 解释更多关于...?
    • 关于 Tuple.create(b.id,b.name)
    • 请写出我看不懂的完整例子
    • is 是一个完整的例子....您将 select 更改为 select Tuple.Create(b.id, b.name) 隐含的 Tuple&lt;int,string&gt; - 并将其与 .ToList() 一起存储前。然后;当你把它拿回来时,你会投射到你所知道的 - List&lt;Tuple&lt;int,string&gt;&gt;
    【解决方案5】:

    Session["CountryCompany"] 不会持有List&lt;PropertyCompany&gt;,而是我认为像&lt;string,string&gt; 这样的通用列表。执行ToList()部分时检查类型。

    【讨论】:

      【解决方案6】:

      对于匿名类型,你可以做的就是在 select 中使用它们,然后在 foreach 中使用它们。

      var q = _someCollection.Where(c => c.Id == 100).Select (new { Id = c.Id, Name = c.Name});
      
      foreach(var p in q) {
         var id = p.Id;
      }
      

      这会起作用,因为实际上,编译器知道 new { Age = c.Age, Position = c.Pos} 的类型(它会为此生成附加信息)。

      一旦你转换为 List,然后你已经转换为对象(放入 Session),你将无法再获得类型信息。

      我会介绍一些真正的类型CountryInfo 并更改查询;

      var q = _someCollection.Where(c => c.Id == 100).Select(new ContryInfo { Id = c.Id, Name = c.Name}).
      
      Session["a"] = q.ToList();
      
      var list = (IList<ContryInfo>)Session["a"];
      

      【讨论】:

      • q.tolist() 说错误:不允许在查询中显式构造实体类型“PropertyCompany”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多