English 中文(简体)
选择使用CRM 2011动态实体的记录百分比
原标题:Select a percentage of records using CRM 2011 Dynamic Entity

I m 开发服务,通过动态实体(如Microsoft)消费2011年CRM数据。 Xrm.Sdk.Entity, 过时约束的方法。 我故意不使用Xrm.cs方法(具有早期约束力),企图使我的解决办法具有通用性。

此外,我希望避免直接连接CRM数据库(例如EDMX),因为这将阻止我的解决办法供一个东道国的CRM(例如,没有直接进入银行)。

我有以下(简化)要求,我确实在努力满足甄选标准:

A random 7% of records needs to be selected (and updated).

选修标准比较容易——我知道如何选择随机记录的百分比。 类似:

SELECT TOP 7 PERCENT * FROM
(
    SELECT TOP 1000 NEWID() AS Foo, [someColumns]
    FROM [someTable]
)
AS Bar ORDER BY Bar.Foo ASC

这完全可行。 我收集了相当于《准则》的内容:

from e in someEntities
orderby Guid.NewGuid()
select e;

然而,我不知道如何使用准则Q与CRM2011动态实体——相反,他们坚持使用一些限制性的QueryExpression等级/syntax或fetchXML,见,这页(MSDN)

我确定了满足这一要求的以下选择:

  1. 利用有活力的实体将整个记录重新列入清单,然后简单地选择按指数进行随机选择。 然而,这涉及在互联网数据服务中恢复多达10 000份记录,这可能是缓慢/不安全/不稳定的。

  2. 采用fetchXML声明。 不幸的是,我不知道有FetchXML,因此我不知道它是否能够做像COUNT、TOP、PERCENT或NewID()。

  3. Use Xrm.cs and LINQ, or use a Stored Procedure, or a SQL view. All of these options mean tying the solution down to either direct database connectivity and/or early binding, which is not desirable.

  4. Say no to the customer.

任何建议都会受到高度赞赏。 fetchXML能够执行这一询问? 是否有更好的办法这样做?

最佳回答

FetchXML不支持这种做法,因此,你要么被降到1或3。 而你是正确的,只有3人才能在《预言》文本中工作,因为你可以与CRM在线产品直接连接。 然而,除非你绝对相信客户将进入在线客户关系管理系统,否则我会这样做。 如果你必须接手1,你至少可以把返回的栏目限制在只限为记录的统一格式上,以减少有效载荷的规模。 然后,在你选择随机记录时,如果需要,就只增加一栏(当然,这可能会因为“聊天”而放缓,这取决于你处理多少随机记录。

问题回答

此时此刻,CRM(2011年)可向您提供L.K.和其他LINQ供应商能够提供的质疑程度,因此,我确实认为,如果他/她想要这种灵活性的话,你会希望对客户的检验,向上调。

尽管如此,第1号方法的备选案文是,而不是一劳永逸地选择你的随机组合,然后从实体中抽取一行的随机组合,直到你们想要的行数为止。 这种方法的下游之处在于,不向非行发出呼吁,而是有许多,降低了整体检索速度。 一个POC如下。

As for #2, I believe it s possible to handle all of your request, with some degree of success, using fetchXml. In fact, the only way to get aggregated data is by using fetchXml, and it also supports paging.

As for #3, native SQL is your best bet to get everything you want out of your data at this point, but that notwithstanding, while the LINQ provider is limited, it s a lot easier to transition SQL statements to LINQ than to fetchXML, and it does support late-binding/dynamic entities.

//create a list of random numbers
List<int> randomNumbers = new List<int>();

//declare a percentage of records you d like to retrieve
double pctg = 0.07;

//use FetchXML to count the # of rows in the table
string fetchXml = @"<fetch aggregate= true >
<entity name= salesorder >
<attribute name= salesorderid  aggregate= count  alias= countIds  distinct= false  />
</entity>
</fetch>";
EntityCollection result = _service.RetrieveMultiple(new FetchExpression(fetchXml));
int rowCount = int.Parse(result.Entities[0].FormattedValues["countIds"].Replace(",", ""));

//initalize the random number list for paging
for (int i = 0; i < Math.Ceiling(pctg * rowCount); i++)
{
    randomNumbers.Add((new Random(unchecked((int)(DateTime.Now.Ticks >> i)))).Next(rowCount - 1));
}
randomNumbers.Sort();

//page through the rows one at a time until you have the number of rows you want
using (OrganizationServiceContext osc = new OrganizationServiceContext(_service))
{
    foreach (int r in randomNumbers)
    {
        foreach (var er in (from c in osc.CreateQuery("salesorder")
                            //not especially useful to use the orderby option as you can only order by entity attributes
                            //orderby c.GetAttributeValue<string>("name")
                            select new
                            {
                                name = c.GetAttributeValue<string>("name")
                            }).Skip(r).Take(1))
        {
            Console.WriteLine(er.name);
        }

    }
}




相关问题
How to return TEntity

How to create a property that return TEntity object for dataContext.GetTable parameter. The example code shown below. Thank You. public IQueryable<Order> FetchAll() { dataContext.GetTable&...

Entity Framework and Load Testing

I am having a tough time to understand why this code is failing I have a test method IUnitOfWork unitofwork = EFUnitOfWork.CreateInstance(); IRepository<InformationRequest> ...

implementing core data to an existing iPhone-project

I´ve some trouble with implementing Core Data to my existing iPhone-Project. First I wanna give you a more detailed view on it: Some of my classes are nested into each other: The class "Game" has an ...

linqpad 4.0 and code only

How can I use linqpad with code only in ef 4. I mean how to reference metadata when there is no edmx file?

Database visualization tool

I just watched a session of PDC09 about new features of Entity framework in .NET 4. Video page: http://microsoftpdc.com/Sessions/FT10 in the video, (seek to minute 7) presenter used a database ...

Entity Framework CreatedBy fields not updating

Using Entity Framework I have the fields: o CreatedOn (datetime) o CreatedBy (nvarchar(50)) o ModifiedOn (datetime) o ModifiedBy (nvarchar(50)) When I add data to my table it is not adding/...

Entity Framework - Many to many question

I have a table called ASB and a table called PeopleInvolved. There is a junction table called PeopleInvolved_ASB which simply contains an ASBID and a PeopleInvolvedID column. The columns act as a ...

How to fluent-map this (using fluent nhibernate)?

I have two tables in my database "Styles" and "BannedStyles". They have a reference via the ItemNo. Now styles can be banned per store. So if style x is banned at store Y then its very possible that ...

热门标签