English 中文(简体)
数据内容也产生了同样的问题。 翻译
原标题:Problem getting same result from DataContext.Translate

This question is a follow up to this question: How to create a list from two values

考虑这一法典:

class MainClass() 
{
   string MainKey {get;set;}
   string MainName {get;set;}
   IEnumerable<SmallObject> MainList {get;set} 
}

class SmallObject() 
{
   string SmallKey {get;set} 
} 

并且

var mainQuery = (from v from DataContext.myTable
                 select v);

var myQuery = (from v in mainQuery
               select new MainClass()
               {
                  MainKey = v.Field1,
                  MainName = v.Field2,
                  MainList = new []
                  {
                     new SmallObject { SmallKey = v.Field3 },
                     new SmallObject { SmallKey = v.Field4 },
                  }
                });


var result1 = myQuery.ToList();

//Changing datatypes for optimization reasons in SQLServer2000
var cmd = DataContext.GetCommand(myQuery);
foreach (System.Data.Common.DbParameter param in cmd.Parameters)
{
  // nvarchar -> varchar
  // decimal -> numeric
}
var result2 = DataContext.Translate<MainClass>(cmd.ExecuteReader()).ToList();

result1.MainList is OK

结果2 首席大法官

最初的询问在SQerver2000上运行非常缓慢,在改变数据类型时(Linq使用Nvarchar和decimal,因为我的数据库使用斜体和数字)我定下来。

因此,我希望结果2与结果1相同,但在进行数据汇编时,情况并非如此。 与此类似。

在这里取得同样结果的任何想法?

我也尝试过匿名的类型,例如:

IEnumerable<object> MainList {get;set;}
...
MainList = new []
{
   new { SmallKey = v.Field3},
   new { SmallKey = v.Field4},
}

但结果相同:

问题回答

我认为,你在要求翻译过多。

如果我正确理解你的话,那么第一个问题(主要问题)就太缓慢,所以我想取而代之。

我将设立一个更简单的临时班级,像一个这样。

 public class TmpClass
 {
    public string Field1 {get;set;}
    public string Field2 {get;set;}
    public string Field3 {get;set;}
    public string Field4 {get;set;}
 } 

一旦名单采用这种格式,你就可以利用第二个问题将其改为主要地图册。

只是一个值得关注的问题,林克的 s木产量与你的定制版本之间有何区别? 除非有人 cast,否则我不会期望这种质询需要优化。

我将使用。 无数推广方法 基本改为<代码>I 质量。 缩略语 您可以通过打电话<代码>ToArray()或ToList(,但AsE amountable( 魔法>,使您回到。 IQueryable, 电话As Queryable(

So probably doing the following will work for you:

var result1 = DataContext.myTable.AsEnumerable()
  .Select(v=> new MainClass {
                  MainKey = v.Field1,
                  MainName = v.Field2,
                  MainList = new []
                  {
                     new SmallObject { SmallKey = v.Field3 },
                     new SmallObject { SmallKey = v.Field4 },
                  }
         });




相关问题
IEnumerable to array of parameter

Using linq? and XML is there a way to convert this IEnumerable to a string array of the value parameter? List<string> idList = new List<string>(); foreach (XElement idElement in word....

linq query for tag system - search for multiple tags

I have two tables, Tags(tagid, postid, tagname) and posts(postid, name, ...) now i want to make a query that returns me all posts that have a generic amount of tags. like: i want all posts that have ...

Linq operations against a List of Hashtables?

I m working with a set of legacy DAO code that returns an IList, where each Hashtable represents the row of a dynamically executed SQL query. For example, the List might contain the following records/...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

How to filter duplicate list items

i have list of items IList with data that looks list this: GenId TestMode 1 0 1 1 3 0 3 1 4 NULL 2 NULL i want to remove the index ...

C# Grouping/Sorting a Generic List<> using LINQ

Im looking to group and sort a Generic List<>. I have a list of objects representing files and each of these objects has a FileName, FileType and FileDate property. FileType is defined as an ...

热门标签